Skip to content

Commit

Permalink
no longer use the problematic ngx_strXcmp because it may cause invali…
Browse files Browse the repository at this point in the history
…d reads, thanks Piotr Sikora.
  • Loading branch information
agentzh committed Sep 14, 2010
1 parent d1aa2c5 commit a00a637
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 104 deletions.
18 changes: 7 additions & 11 deletions src/ngx_http_drizzle_keepalive.c
Expand Up @@ -35,7 +35,7 @@ ngx_http_upstream_drizzle_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,

for (i = 1; i < cf->args->nelts; i++) {

if (ngx_strncmp(value[i].data, "max=", sizeof("max=") - 1)
if (ngx_http_drizzle_strcmp_const(value[i].data, "max=")
== 0)
{
len = value[i].len - (sizeof("max=") - 1);
Expand All @@ -57,21 +57,19 @@ ngx_http_upstream_drizzle_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
continue;
}

if (ngx_strncmp(value[i].data, "mode=", sizeof("mode=") - 1)
== 0)
{
if (ngx_http_drizzle_strcmp_const(value[i].data, "mode=") == 0) {
len = value[i].len - (sizeof("mode=") - 1);
data = &value[i].data[sizeof("mode=") - 1];

switch (len) {
case 6:
if (ngx_str6cmp(data, 's', 'i', 'n', 'g', 'l', 'e')) {
if (ngx_http_drizzle_strcmp_const(data, "single") == 0) {
dscf->single = 1;
}
break;

case 5:
if (ngx_str5cmp(data, 'm', 'u', 'l', 't', 'i')) {
if (ngx_http_drizzle_strcmp_const(data, "multi") == 0) {
dscf->single = 0;
}
break;
Expand All @@ -88,17 +86,15 @@ ngx_http_upstream_drizzle_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
continue;
}

if (ngx_strncmp(value[i].data, "overflow=", sizeof("overflow=") - 1)
== 0)
{
if (ngx_http_drizzle_strcmp_const(value[i].data, "overflow=") == 0) {
len = value[i].len - (sizeof("overflow=") - 1);
data = &value[i].data[sizeof("overflow=") - 1];

switch (len) {
case 6:
if (ngx_str6cmp(data, 'r', 'e', 'j', 'e', 'c', 't')) {
if (ngx_http_drizzle_strcmp_const(data, "reject") == 0) {
dscf->overflow = drizzle_keepalive_overflow_reject;
} else if (ngx_str6cmp(data, 'i', 'g', 'n', 'o', 'r', 'e')) {
} else if (ngx_http_drizzle_strcmp_const(data, "ignore") == 0) {
dscf->overflow = drizzle_keepalive_overflow_ignore;
}
break;
Expand Down
92 changes: 2 additions & 90 deletions src/ngx_http_drizzle_util.h
Expand Up @@ -7,6 +7,8 @@
#include <ngx_core.h>
#include <ngx_http.h>

#define ngx_http_drizzle_strcmp_const(a, b) \
ngx_strncmp(a, b, sizeof(b) - 1)

void ngx_http_upstream_dbd_init(ngx_http_request_t *r);

Expand All @@ -26,96 +28,6 @@ ngx_int_t ngx_http_upstream_drizzle_test_connect(ngx_connection_t *c);

#define ngx_http_drizzle_nelems(x) (sizeof(x) / sizeof(x[0]))

#ifndef ngx_str3cmp

# define ngx_str3cmp(m, c0, c1, c2) \
m[0] == c0 && m[1] == c1 && m[2] == c2

#endif /* ngx_str3cmp */


#ifndef ngx_str4cmp

# if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)

# define ngx_str4cmp(m, c0, c1, c2, c3) \
*(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)

# else

# define ngx_str4cmp(m, c0, c1, c2, c3) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3

# endif

#endif /* ngx_str4cmp */


#ifndef ngx_str5cmp

# if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)

# define ngx_str5cmp(m, c0, c1, c2, c3, c4) \
*(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& m[4] == c4

# else

# define ngx_str5cmp(m, c0, c1, c2, c3, c4) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 && m[4] == c4

# endif

#endif /* ngx_str5cmp */


#ifndef ngx_str6cmp

# if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)

# define ngx_str6cmp(m, c0, c1, c2, c3, c4, c5) \
*(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& (((uint32_t *) m)[1] & 0xffff) == ((c5 << 8) | c4)

# else

# define ngx_str6cmp(m, c0, c1, c2, c3, c4, c5) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \
&& m[4] == c4 && m[5] == c5

# endif

#endif /* ngx_str6cmp */


#ifndef ngx_str7cmp

# define ngx_str7cmp(m, c0, c1, c2, c3, c4, c5, c6) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \
&& m[4] == c4 && m[5] == c5 && m[6] == c6

#endif /* ngx_str7cmp */


#ifndef ngx_str9cmp

# if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)

# define ngx_str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \
*(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0) \
&& ((uint32_t *) m)[1] == ((c7 << 24) | (c6 << 16) | (c5 << 8) | c4) \
&& m[8] == c8

# else

# define ngx_str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \
&& m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7 && m[8] == c8

# endif


#endif /* ngx_str9cmp */

#endif /* NGX_HTTP_DRIZZLE_UTIL_H */

6 changes: 3 additions & 3 deletions src/ngx_http_upstream_drizzle.c
Expand Up @@ -204,7 +204,7 @@ ngx_http_upstream_drizzle_server(ngx_conf_t *cf, ngx_command_t *cmd,

switch (protocol.len) {
case 5:
if (ngx_str5cmp(protocol.data, 'm', 'y', 's', 'q', 'l')) {
if (ngx_http_drizzle_strcmp_const(protocol.data, "mysql") == 0) {
ds->protocol = ngx_http_mysql_protocol;
} else {
continue;
Expand All @@ -213,8 +213,8 @@ ngx_http_upstream_drizzle_server(ngx_conf_t *cf, ngx_command_t *cmd,
break;

case 7:
if ( ! ngx_str7cmp(protocol.data,
'd', 'r', 'i', 'z', 'z', 'l', 'e'))
if (ngx_http_drizzle_strcmp_const(protocol.data,
"drizzle") != 0)
{
continue;
}
Expand Down

0 comments on commit a00a637

Please sign in to comment.