Move ws_return macros to ws_assert.h

This commit is contained in:
João Valverde 2023-09-16 07:06:55 +01:00
parent 3516d3a181
commit cfee53e49a
5 changed files with 33 additions and 51 deletions

View File

@ -139,7 +139,6 @@ set(WSUTIL_PUBLIC_HEADERS
ws_mempbrk_int.h
ws_pipe.h
ws_roundup.h
ws_return.h
ws_strptime.h
wsgcrypt.h
wsjson.h

View File

@ -10,7 +10,6 @@
#include "regex.h"
#include <wsutil/ws_return.h>
#include <wsutil/str_util.h>
#include <pcre2.h>

View File

@ -19,7 +19,6 @@
#include <wsutil/wslog.h>
#include <wsutil/inet_addr.h>
#include <wsutil/pint.h>
#include <wsutil/ws_return.h>
#include <wsutil/time_util.h>
/*

View File

@ -15,6 +15,7 @@
#include <stdbool.h>
#include <string.h>
#include <wsutil/wslog.h>
#include <wsutil/wmem/wmem.h>
#ifdef WS_DEBUG
#define _ASSERT_ENABLED true
@ -86,6 +87,38 @@ extern "C" {
#define ws_assert_not_reached() \
ws_error("assertion \"not reached\" failed")
/*
* These macros can be used as an alternative to ws_assert() to
* assert some condition on function arguments. This must only be used
* to catch programming errors, in situations where an assertion is
* appropriate. And it should only be used if failing the condition
* doesn't necessarily lead to an inconsistent state for the program.
*
* It is possible to set the fatal log domain to "InvalidArg" to abort
* execution for debugging purposes, if one of these checks fail.
*/
#define ws_warn_badarg(str) \
ws_log_full(LOG_DOMAIN_EINVAL, LOG_LEVEL_INFO, \
__FILE__, __LINE__, __func__, \
"bad argument: %s", str)
#define ws_return_str_if(expr, scope) \
do { \
if (expr) { \
ws_warn_badarg(#expr); \
return wmem_strdup(scope, "(invalid argument)"); \
} \
} while (0)
#define ws_return_val_if(expr, val) \
do { \
if (expr) { \
ws_warn_badarg(#expr); \
return (val); \
} \
} while (0)
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -1,48 +0,0 @@
/** @file
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WS_RETURN_H__
#define __WS_RETURN_H__
#include <wsutil/wslog.h>
#include <wsutil/wmem/wmem.h>
/*
* These macros can be used as an alternative to ws_assert() to
* assert some condition on function arguments. This must only be used
* to catch programming errors, in situations where an assertion is
* appropriate. And it should only be used if failing the condition
* doesn't necessarily lead to an inconsistent state for the program.
*
* It is possible to set the fatal log domain to "InvalidArg" to abort
* execution for debugging purposes, if one of these checks fail.
*/
#define ws_warn_badarg(str) \
ws_log_full(LOG_DOMAIN_EINVAL, LOG_LEVEL_INFO, \
__FILE__, __LINE__, __func__, \
"bad argument: %s", str)
#define ws_return_str_if(expr, scope) \
do { \
if (expr) { \
ws_warn_badarg(#expr); \
return wmem_strdup(scope, "(invalid argument)"); \
} \
} while (0)
#define ws_return_val_if(expr, val) \
do { \
if (expr) { \
ws_warn_badarg(#expr); \
return (val); \
} \
} while (0)
#endif /* WS_RETURN_H_ */