Skip to content

Commit

Permalink
Don't declare unterminated character arrays/strings
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ac000 committed Jan 21, 2025
1 parent b4201ab commit fe37226
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/nxt_http_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/nxt_sprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
Expand Down
15 changes: 7 additions & 8 deletions src/nxt_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit fe37226

Please sign in to comment.