From fe372269737132986a534e098900e1c9c1d7a6d6 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Tue, 21 Jan 2025 00:57:16 +0000 Subject: [PATCH] Don't declare unterminated character arrays/strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When building with the upcoming GCC 15, we are hit by the new Wunterminated-string-initialization compiler warning. E.g. src/nxt_string.c: In function ‘nxt_encode_uri’: src/nxt_string.c:601:36: error: initializer-string for array of ‘unsigned char’ is too long [-Werror=unterminated-string-initialization] 601 | static const u_char hex[16] = "0123456789ABCDEF"; | ^~~~~~~~~~~~~~~~~~ We have created a character array containing 16 elements, but is not NUL terminated. While this type of (lookup/translation table) use case is perfectly valid and we could simply disable the warning, it is probably still a useful warning to have enabled. There is a 'nonstring' attribute, but that doesn't influence this warning. Lets just do what nginx does and create them NUL terminated. Signed-off-by: Andrew Clayton --- src/nxt_http_parse.c | 11 +++++++---- src/nxt_sprintf.c | 4 ++-- src/nxt_string.c | 15 +++++++-------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/nxt_http_parse.c b/src/nxt_http_parse.c index dd490e729..e7c2af5fd 100644 --- a/src/nxt_http_parse.c +++ b/src/nxt_http_parse.c @@ -164,8 +164,10 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, nxt_http_ver_t ver; nxt_http_target_traps_e trap; - static const nxt_http_ver_t http11 = { "HTTP/1.1" }; - static const nxt_http_ver_t http10 = { "HTTP/1.0" }; + static const nxt_http_ver_t http11 = + {{ 'H', 'T', 'T', 'P', '/', '1', '.', '1' }}; + static const nxt_http_ver_t http10 = + {{ 'H', 'T', 'T', 'P', '/', '1', '.', '0' }}; p = *pos; @@ -516,7 +518,8 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos, size_t len; uint32_t hash; - static const u_char normal[256] nxt_aligned(64) = + /* The last '\0' is not needed because the string is NUL terminated */ + static const u_char normal[] nxt_aligned(64) = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" /* \s ! " # $ % & ' ( ) * + , . / : ; < = > ? */ "\0\1\0\1\1\1\1\1\0\0\1\1\0" "-" "\1\0" "0123456789" "\0\0\0\0\0\0" @@ -529,7 +532,7 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" - "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; + "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; p = *pos + rp->field_name.length; hash = rp->field_hash; diff --git a/src/nxt_sprintf.c b/src/nxt_sprintf.c index 875f43a59..2d1541895 100644 --- a/src/nxt_sprintf.c +++ b/src/nxt_sprintf.c @@ -112,8 +112,8 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) nxt_sprintf_t spf; nxt_file_name_t *fn; - static const u_char hexadecimal[16] = "0123456789abcdef"; - static const u_char HEXADECIMAL[16] = "0123456789ABCDEF"; + static const u_char hexadecimal[] = "0123456789abcdef"; + static const u_char HEXADECIMAL[] = "0123456789ABCDEF"; static const u_char nan[] = "[nan]"; static const u_char null[] = "[null]"; static const u_char infinity[] = "[infinity]"; diff --git a/src/nxt_string.c b/src/nxt_string.c index 1ca595a1c..ad9b7aa69 100644 --- a/src/nxt_string.c +++ b/src/nxt_string.c @@ -592,14 +592,15 @@ nxt_decode_uri_plus(u_char *dst, u_char *src, size_t length) } +static const u_char nxt_hex[] = "0123456789ABCDEF"; + + uintptr_t nxt_encode_uri(u_char *dst, u_char *src, size_t length) { u_char *end; nxt_uint_t n; - static const u_char hex[16] = "0123456789ABCDEF"; - end = src + length; if (dst == NULL) { @@ -624,8 +625,8 @@ nxt_encode_uri(u_char *dst, u_char *src, size_t length) if (nxt_uri_escape[*src >> 5] & (1U << (*src & 0x1f))) { *dst++ = '%'; - *dst++ = hex[*src >> 4]; - *dst++ = hex[*src & 0xf]; + *dst++ = nxt_hex[*src >> 4]; + *dst++ = nxt_hex[*src & 0xf]; } else { *dst++ = *src; @@ -644,8 +645,6 @@ nxt_encode_complex_uri(u_char *dst, u_char *src, size_t length) u_char *reserved, *end, ch; nxt_uint_t n; - static const u_char hex[16] = "0123456789ABCDEF"; - reserved = (u_char *) "?#\0"; end = src + length; @@ -689,8 +688,8 @@ nxt_encode_complex_uri(u_char *dst, u_char *src, size_t length) } else { *dst++ = '%'; - *dst++ = hex[ch >> 4]; - *dst++ = hex[ch & 0xf]; + *dst++ = nxt_hex[ch >> 4]; + *dst++ = nxt_hex[ch & 0xf]; continue; } }