Skip to content

Commit

Permalink
Add format attribute to log function and adjust format specifiers
Browse files Browse the repository at this point in the history
Annotate the common logging function sp_log_msgf() with the format
attribute so compilers can check the used format string and passed
arguments for discrepancies.

Adjust the lineno printing by using %zu and the type size_t
consistently.
  • Loading branch information
cgzones authored and jvoisin committed Jun 6, 2024
1 parent 381aa67 commit 849252c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/sp_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ zend_result sp_process_rule(sp_parsed_keyword *parsed_rule, const sp_config_keyw

if (!found_kw) {
zend_string *kwname = zend_string_init(kw->kw, kw->kwlen, 0);
sp_log_err("config", "Unexpected keyword '%s' on line %d", ZSTR_VAL(kwname), kw->lineno);
sp_log_err("config", "Unexpected keyword '%s' on line %zu", ZSTR_VAL(kwname), kw->lineno);
zend_string_release_ex(kwname, 0);
return FAILURE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sp_config_scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef struct {
const char *arg; // optional argument / can be not null terminated
size_t arglen;
sp_argtype argtype;
long lineno;
size_t lineno;
} sp_parsed_keyword;

zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_parsed_keyword*));
Expand Down
32 changes: 16 additions & 16 deletions src/sp_config_scanner.re
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ static void str_dtor(zval *zv) {

// sy_ functions and macros are helpers for the shunting yard algorithm
#define sy_res_push(val) \
if (cond_res_i >= MAX_CONDITIONS) { cs_log_error("condition too complex on line %d", lineno); goto out; } \
if (cond_res_i >= MAX_CONDITIONS) { cs_log_error("condition too complex on line %zu", lineno); goto out; } \
cond_res[cond_res_i++] = val;
#define sy_res_pop() cond_res[--cond_res_i]
#define sy_op_push(op) \
if (cond_op_i >= MAX_CONDITIONS) { cs_log_error("condition too complex on line %d", lineno); goto out; } \
if (cond_op_i >= MAX_CONDITIONS) { cs_log_error("condition too complex on line %zu", lineno); goto out; } \
cond_op[cond_op_i++] = op;
#define sy_op_pop() cond_op[--cond_op_i]
#define sy_op_peek() cond_op[cond_op_i-1]
Expand Down Expand Up @@ -138,7 +138,7 @@ static int sy_apply_op(const char op, const int a, const int b) {
#define SY_APPLY_OP_FROM_STACK() \
char op = sy_op_pop(); \
int unary = (op == '!'); \
if (cond_res_i < (2 - unary)) { cs_log_error("not enough input on line %d", lineno); goto out; } \
if (cond_res_i < (2 - unary)) { cs_log_error("not enough input on line %zu", lineno); goto out; } \
int a = sy_res_pop(); \
int b = unary ? 0 : sy_res_pop(); \
int res = sy_apply_op(op, a, b); \
Expand Down Expand Up @@ -169,7 +169,7 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars
int cond_op_i = 0;

int cond = yycinit;
long lineno = 1;
size_t lineno = 1;

/*!stags:re2c format = 'const char *@@;\n'; */
/*!re2c
Expand All @@ -189,7 +189,7 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars
keyword = [a-zA-Z][a-zA-Z0-9_]*;
string = ["] ("\\"["] | [^"\r\n\x00])* ["];
<init> * { cs_log_error("parser error on line %d", lineno); goto out; }
<init> * { cs_log_error("parser error on line %zu", lineno); goto out; }
<init> whitespace+ { goto yyc_init; }
<init> [;#] [^\r\n\x00]* { goto yyc_init; }
<init> newline { lineno++; goto yyc_init; }
Expand All @@ -212,19 +212,19 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars
<init> ( "@log" | "@info" ) whitespace+ @t1 string @t2 ";" {
if (!cond_res[0]) { goto yyc_init; }
TMPSTR(tmpstr, t2, t1);
cs_log_info("[line %d]: %s", lineno, tmpstr);
cs_log_info("[line %zu]: %s", lineno, tmpstr);
goto yyc_init;
}
<init> ( "@warn" | "@warning" ) whitespace+ @t1 string @t2 ";" {
if (!cond_res[0]) { goto yyc_init; }
TMPSTR(tmpstr, t2, t1);
cs_log_warning("[line %d]: %s", lineno, tmpstr);
cs_log_warning("[line %zu]: %s", lineno, tmpstr);
goto yyc_init;
}
<init> ( "@err" | "@error" ) whitespace+ @t1 string @t2 ";" {
if (!cond_res[0]) { goto yyc_init; }
TMPSTR(tmpstr, t2, t1);
cs_log_error("[line %d]: %s", lineno, tmpstr);
cs_log_error("[line %zu]: %s", lineno, tmpstr);
goto out;
}
Expand All @@ -236,15 +236,15 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars
int is_loaded = (zend_hash_str_find_ptr(&module_registry, t3+1, t4-t3-2) != NULL);
sy_res_push(is_loaded);
} else {
cs_log_error("unknown function in condition on line %d", lineno);
cs_log_error("unknown function in condition on line %zu", lineno);
goto out;
}
goto yyc_cond_op;
}
<cond> @t1 keyword @t2 {
zend_string *tmp = zend_hash_str_find_ptr(&vars, t1, t2-t1);
if (!tmp) {
cs_log_error("unknown variable in condition on line %d", lineno);
cs_log_error("unknown variable in condition on line %zu", lineno);
goto out;
}
sy_res_push(atoi(ZSTR_VAL(tmp)));
Expand Down Expand Up @@ -280,27 +280,27 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars
SY_APPLY_OP_FROM_STACK();
}
if (cond_op_i == 0 || sy_op_peek() != '(') {
cs_log_error("unbalanced parenthesis on line %d", lineno); goto out;
cs_log_error("unbalanced parenthesis on line %zu", lineno); goto out;
}
cond_op_i--;
goto yyc_cond_op;
}
<cond_op> ";" {
while (cond_op_i) {
if (sy_op_peek() == '(') { cs_log_error("unbalanced parenthesis on line %d", lineno); goto out; }
if (sy_op_peek() == '(') { cs_log_error("unbalanced parenthesis on line %zu", lineno); goto out; }
SY_APPLY_OP_FROM_STACK();
}
if (cond_res_i > 1) { cs_log_error("invalid condition on line %d", lineno); goto out; }
if (cond_res_i > 1) { cs_log_error("invalid condition on line %zu", lineno); goto out; }
goto yyc_init;
}
<cond, cond_op> * { cs_log_error("syntax error in condition on line %d", lineno); goto out; }
<cond, cond_op> * { cs_log_error("syntax error in condition on line %zu", lineno); goto out; }
<rule> whitespace+ { goto yyc_rule; }
<rule> newline / ( newline | whitespace )* "." { lineno++; goto yyc_rule; }
<rule> "." @t1 keyword @t2 ( "(" @t3 ( string? | keyword ) @t4 ")" )? {
if (!cond_res[0]) { goto yyc_rule; }
if (kw_i == MAX_KEYWORDS) {
cs_log_error("too many keywords in rule (more than %d) on line %d", MAX_KEYWORDS, lineno);
cs_log_error("too many keywords in rule (more than %d) on line %zu", MAX_KEYWORDS, lineno);
goto out;
}
sp_parsed_keyword kw = {
Expand All @@ -321,7 +321,7 @@ zend_result sp_config_scan(const char *data, zend_result (*process_rule)(sp_pars
} else {
zend_string *tmp = zend_hash_str_find_ptr(&vars, t3, t4-t3);
if (!tmp) {
cs_log_error("unknown variable on line %d", lineno);
cs_log_error("unknown variable on line %zu", lineno);
goto out;
}
kw.arg = ZSTR_VAL(tmp);
Expand Down
2 changes: 1 addition & 1 deletion src/sp_ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static bool /* success */ sp_ini_check(zend_string *const restrict varname, zend
if (entry->msg) {
sp_log_ini_check_violation("%s", ZSTR_VAL(entry->msg));
} else {
sp_log_ini_check_violation("INI value %lld for `%s` out of range", lvalue, ZSTR_VAL(entry->key));
sp_log_ini_check_violation("INI value " ZEND_LONG_FMT " for `%s` out of range", lvalue, ZSTR_VAL(entry->key));
}
return simulation;
}
Expand Down
5 changes: 5 additions & 0 deletions src/sp_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ extern int sp_debug_stderr;
#define GET_SUFFIX(x) (x == 1) ? "st" : ((x == 2) ? "nd" : "th")

const char *get_ipaddr(void);
#if defined __has_attribute
# if __has_attribute (__format__)
__attribute__((__format__(printf, 4, 5)))
# endif
#endif
void sp_log_msgf(char const* const restrict feature, int level, int type, char const* const restrict fmt, ...);
int compute_hash(char const* const restrict filename, char *restrict file_hash);
const zend_string *sp_zval_to_zend_string(const zval *);
Expand Down

0 comments on commit 849252c

Please sign in to comment.