C nginx内存池已损坏?
我有一个库,它要求构造函数中有一个C nginx内存池已损坏?,c,nginx,C,Nginx,我有一个库,它要求构造函数中有一个const char*src。详情如下: MYCLASS::MYCLASS(const char*src) 我试图从nginx模块中使用它来将cookie的值传递给这个库,我得到了一些内存损坏 我不知道我现在做错了什么。调试“cookie”时报告的cookie值始终正确,但u_cookie上报告的值仅在第一个请求时正确,在nginx服务其他请求并且池被其他人以某种方式使用后,返回的值始终包含一个附加字符 ngx_str_t cookie = (ngx_str_t
const char*src
。详情如下:
MYCLASS::MYCLASS(const char*src)
我试图从nginx模块中使用它来将cookie的值传递给这个库,我得到了一些内存损坏
我不知道我现在做错了什么。调试“cookie”时报告的cookie值始终正确,但u_cookie上报告的值仅在第一个请求时正确,在nginx服务其他请求并且池被其他人以某种方式使用后,返回的值始终包含一个附加字符
ngx_str_t cookie = (ngx_str_t)ngx_string("THECOOKIE");
ngx_int_t location;
ngx_str_t cookie_value;
location = ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &cookie, &cookie_value);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"cookie: \"%V\"", &cookie_value);
u_char *u_cookie = (u_char *)ngx_pcalloc(r->pool, cookie_value.len);
ngx_copy(u_cookie, cookie_value.data, cookie_value.len);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,"u_cookie: \"%s\"", u_cookie);
MYCLASS myclass( (const char*)u_cookie );
日志中的输出如下所示
2015/09/30 04:49:32[调试]cookie:“405JanIN9ztP/snEzyucny8KdgN”
2015/09/30 04:49:32[调试]u_cookie:“405JanIN9ztP/snEzyucny8KdgNo”
这是因为
ngx_copy
只会复制N个字符(其中N-是cookie的长度)。但是您还需要设置空字符串终止符!像这样:
u_char *u_cookie = (u_char *)ngx_pcalloc(r->pool, cookie_value.len + 1); // +1 for null char
ngx_copy(u_cookie, cookie_value.data, cookie_value.len);
u_cookie[cookie_value.len] = 0
我最终从一个朋友那里找到了相同的答案,但稍晚收到了这封电子邮件通知。唯一的区别是他的解决方案使用了:u_cookie[cookie_value.len]='\0',结果是相同的。