From 952bcc50bfc5bd651a56fd97aa6f1f3c3e214071 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 30 Apr 2022 19:20:23 +0200 Subject: Fixed #define style. We had a mix of styles for declaring function-like macros: Style A: #define \ foo() \ do { \ ... \ } while (0) Style B: #define foo() \ do { \ ... \ } while (0) We had a similar number of occurences of each style: $ grep -rnI '^\w*(.*\\' | wc -l 244 $ grep -rn 'define.*(.*)' | wc -l 239 (Those regexes aren't perfect, but a very decent approximation.) Real examples: $ find src -type f | xargs sed -n '/^nxt_double_is_zero/,/^$/p' nxt_double_is_zero(f) \ (fabs(f) <= FLT_EPSILON) $ find src -type f | xargs sed -n '/define nxt_http_field_set/,/^$/p' #define nxt_http_field_set(_field, _name, _value) \ do { \ (_field)->name_length = nxt_length(_name); \ (_field)->value_length = nxt_length(_value); \ (_field)->name = (u_char *) _name; \ (_field)->value = (u_char *) _value; \ } while (0) I'd like to standardize on a single style for them, and IMO, having the identifier in the same line as #define is a better option for the following reasons: - Programmers are used to `#define foo() ...` (readability). - One less line of code. - The program for finding them is really simple (see below). function grep_ngx_func() { if (($# != 1)); then >&2 echo "Usage: ${FUNCNAME[0]} "; return 1; fi; find src -type f \ | grep '\.[ch]$' \ | xargs grep -l "$1" \ | sort \ | xargs pcregrep -Mn "(?s)^\$[\w\s*]+?^$1\(.*?^}"; find src -type f \ | grep '\.[ch]$' \ | xargs grep -l "$1" \ | sort \ | xargs pcregrep -Mn "(?s)define $1\(.*?^$" \ | sed -E '1s/^[^:]+:[0-9]+:/&\n\n/'; } $ grep_ngx_func Usage: grep_ngx_func $ grep_ngx_func nxt_http_field_set src/nxt_http.h:98: #define nxt_http_field_set(_field, _name, _value) \ do { \ (_field)->name_length = nxt_length(_name); \ (_field)->value_length = nxt_length(_value); \ (_field)->name = (u_char *) _name; \ (_field)->value = (u_char *) _value; \ } while (0) $ grep_ngx_func nxt_sprintf src/nxt_sprintf.c:56: u_char * nxt_cdecl nxt_sprintf(u_char *buf, u_char *end, const char *fmt, ...) { u_char *p; va_list args; va_start(args, fmt); p = nxt_vsprintf(buf, end, fmt, args); va_end(args); return p; } ................ Scripted change: ................ $ find src -type f \ | grep '\.[ch]$' \ | xargs sed -i '/define *\\$/{N;s/ *\\\n/ /;s/ //}' --- src/nxt_sprintf.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nxt_sprintf.c') diff --git a/src/nxt_sprintf.c b/src/nxt_sprintf.c index 9557b327..50705ede 100644 --- a/src/nxt_sprintf.c +++ b/src/nxt_sprintf.c @@ -90,8 +90,7 @@ static u_char *nxt_number(nxt_sprintf_t *spf, u_char *buf, double n); /* A right way of "f == 0.0". */ -#define \ -nxt_double_is_zero(f) \ +#define nxt_double_is_zero(f) \ (fabs(f) <= FLT_EPSILON) -- cgit From 27ca67f0df6c7b606c8496b1c57434032b2a577c Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 18 May 2022 12:49:52 +0200 Subject: Added const to remove unnecessary casts. Casts are usually very dangerous, disabling most compiler warnings and basically removing type safety. This change adds 'const' to a pointer where we don't need to write, improving type safety, and that also allows removing some casts. --- src/nxt_sprintf.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/nxt_sprintf.c') diff --git a/src/nxt_sprintf.c b/src/nxt_sprintf.c index 50705ede..90d74335 100644 --- a/src/nxt_sprintf.c +++ b/src/nxt_sprintf.c @@ -97,7 +97,6 @@ static u_char *nxt_number(nxt_sprintf_t *spf, u_char *buf, double n); u_char * nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) { - u_char *p; int d; double f, i; size_t length; @@ -109,6 +108,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) nxt_msec_t ms; nxt_nsec_t ns; nxt_bool_t sign; + const u_char *p; nxt_sprintf_t spf; nxt_file_name_t *fn; @@ -150,7 +150,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) continue; case 's': - p = va_arg(args, u_char *); + p = va_arg(args, const u_char *); if (nxt_fast_path(p != NULL)) { while (*p != '\0' && buf < end) { @@ -168,7 +168,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) if (*fmt == 's') { fmt++; - p = va_arg(args, u_char *); + p = va_arg(args, const u_char *); if (nxt_fast_path(p != NULL)) { goto copy; @@ -378,13 +378,13 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) } if (nxt_slow_path(isnan(f))) { - p = (u_char *) nan; + p = nan; length = nxt_length(nan); goto copy; } else if (nxt_slow_path(isinf(f))) { - p = (u_char *) infinity; + p = infinity; length = nxt_length(infinity); goto copy; -- cgit From caa05887ff70bbd6338b129959a234ef56f1a287 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Wed, 1 Jun 2022 16:40:34 +0100 Subject: Logging a NULL pointer instead of passing it in the memcpy(). --- src/nxt_sprintf.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/nxt_sprintf.c') diff --git a/src/nxt_sprintf.c b/src/nxt_sprintf.c index 90d74335..9c8e27ed 100644 --- a/src/nxt_sprintf.c +++ b/src/nxt_sprintf.c @@ -115,6 +115,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) static const u_char hexadecimal[16] = "0123456789abcdef"; static const u_char HEXADECIMAL[16] = "0123456789ABCDEF"; static const u_char nan[] = "[nan]"; + static const u_char null[] = "[null]"; static const u_char infinity[] = "[infinity]"; spf.end = end; @@ -150,15 +151,18 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) continue; case 's': + fmt++; + p = va_arg(args, const u_char *); - if (nxt_fast_path(p != NULL)) { - while (*p != '\0' && buf < end) { - *buf++ = *p++; - } + if (nxt_slow_path(p == NULL)) { + goto copy; + } + + while (*p != '\0' && buf < end) { + *buf++ = *p++; } - fmt++; continue; case '*': @@ -170,9 +174,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) fmt++; p = va_arg(args, const u_char *); - if (nxt_fast_path(p != NULL)) { - goto copy; - } + goto copy; } continue; @@ -554,7 +556,15 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) copy: - buf = nxt_cpymem(buf, p, nxt_min((size_t) (end - buf), length)); + if (nxt_slow_path(p == NULL)) { + p = null; + length = nxt_length(null); + + } else { + length = nxt_min((size_t) (end - buf), length); + } + + buf = nxt_cpymem(buf, p, length); continue; } -- cgit