Skip to content

Commit 179e2e8

Browse files
committed
bugfix: HEAD requests that lead to a cache hits would cause memory issues like invalid reads.
1 parent 7d00400 commit 179e2e8

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

src/ngx_http_srcache_fetch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
174174
cl = ngx_alloc_chain_link(r->pool);
175175
cl->buf = ngx_calloc_buf(r->pool);
176176
cl->buf->last_buf = 1;
177+
cl->next = NULL;
177178

178179
rc = ngx_http_srcache_next_body_filter(r, cl);
179180

@@ -215,6 +216,7 @@ ngx_http_srcache_access_handler(ngx_http_request_t *r)
215216

216217
ph = cmcf->phase_engine.handlers;
217218
cur_ph = &ph[r->phase_handler];
219+
218220
last_ph = &ph[cur_ph->next - 1];
219221

220222
if (cur_ph < last_ph) {

t/methods.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Test::Nginx::Socket;
55

66
#repeat_each(2);
77

8-
plan tests => repeat_each() * 4 * blocks();
8+
plan tests => repeat_each() * (4 * blocks() + 1);
99

1010
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1111

@@ -194,6 +194,8 @@ HEAD /foo
194194
Content-Type: text/css
195195
Content-Length: 0
196196
--- response_body
197+
--- no_error_log
198+
[error]
197199
198200
199201

t/satisfy.t

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# vi:filetype=
2+
3+
use lib 'lib';
4+
use Test::Nginx::Socket;
5+
6+
#repeat_each(2);
7+
8+
plan tests => repeat_each() * (3 * blocks() + 2);
9+
10+
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
11+
12+
#master_on();
13+
no_shuffle();
14+
15+
run_tests();
16+
17+
__DATA__
18+
19+
=== TEST 1: flush all
20+
--- config
21+
location /flush {
22+
set $memc_cmd 'flush_all';
23+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
24+
}
25+
--- response_headers
26+
Content-Type: text/plain
27+
Content-Length: 4
28+
--- request
29+
GET /flush
30+
--- response_body eval: "OK\r\n"
31+
32+
33+
34+
=== TEST 2: basic fetch (cache miss) - deny all
35+
--- config
36+
location /foo {
37+
satisfy any;
38+
39+
deny all;
40+
41+
default_type text/css;
42+
srcache_fetch GET /memc $uri;
43+
srcache_store PUT /memc $uri;
44+
45+
echo hello;
46+
}
47+
48+
location /memc {
49+
internal;
50+
51+
set $memc_key $query_string;
52+
set $memc_exptime 300;
53+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
54+
}
55+
--- request
56+
GET /foo
57+
--- response_headers
58+
--- response_body_like: 403 Forbidden
59+
--- error_code: 403
60+
61+
62+
63+
=== TEST 3: basic fetch (cache miss)
64+
--- config
65+
location /foo {
66+
satisfy any;
67+
68+
default_type text/css;
69+
srcache_fetch GET /memc $uri;
70+
srcache_store PUT /memc $uri;
71+
72+
echo hello;
73+
}
74+
75+
location /memc {
76+
internal;
77+
78+
set $memc_key $query_string;
79+
set $memc_exptime 300;
80+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
81+
}
82+
--- request
83+
GET /foo
84+
--- response_headers
85+
Content-Type: text/css
86+
Content-Length:
87+
--- response_body
88+
hello
89+
90+
91+
92+
=== TEST 4: basic fetch (cache hit)
93+
--- config
94+
location /foo {
95+
satisfy any;
96+
97+
default_type text/css;
98+
99+
srcache_fetch GET /memc $uri;
100+
srcache_store PUT /memc $uri;
101+
102+
echo world;
103+
}
104+
105+
location /memc {
106+
internal;
107+
108+
set $memc_key $query_string;
109+
set $memc_exptime 300;
110+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
111+
}
112+
--- request
113+
GET /foo
114+
--- response_headers
115+
Content-Type: text/css
116+
Content-Length: 6
117+
--- response_body
118+
hello
119+

t/timeout.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ run_tests();
1616

1717
__DATA__
1818
19-
=== TEST 2: basic fetch (cache miss)
19+
=== TEST 1: basic fetch (cache miss)
2020
--- config
2121
error_page 500 502 503 504 /50x.html;
2222
location = /50x.html {

util/build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ ngx-build $force $version \
3131
--add-module=$root/../postgres-nginx-module \
3232
--add-module=$root/../memc-nginx-module \
3333
--add-module=$root/../ndk-nginx-module \
34+
--with-select_module \
35+
--with-poll_module \
36+
--with-rtsig_module \
3437
--with-debug
3538
#--add-module=/home/agentz/git/dodo/utils/dodo-hook \
3639
#--add-module=$home/work/ngx_http_auth_request-0.1 #\

valgrind.suppress

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
{
2+
<insert_a_suppression_name_here>
3+
Memcheck:Leak
4+
fun:malloc
5+
fun:ngx_calloc
6+
fun:ngx_event_process_init
7+
fun:ngx_single_process_cycle
8+
fun:main
9+
}
110
{
211
<insert_a_suppression_name_here>
312
exp-sgcheck:SorG

0 commit comments

Comments
 (0)