c - printf() seems to be destroying my data -



c - printf() seems to be destroying my data -

i'm writing nginx module in c , having super bizarre results. i've extracted function module test output relevant nginx type/macro definitions.

i'm building struct in build_key_hash_pair function, doing printf() on contents in main. when printf info within inner function, main's output valid. when remove printf within inner function, main prints empty string. confusing because after function phone call build_key_hash_pair not operating on info except display it. here code:

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct ngx_str_t { size_t len; char *data; } ngx_str_t; typedef uintptr_t ngx_uint_t; typedef struct key_hash_pair { ngx_uint_t hash; ngx_str_t key; } key_hash_pair; #define ngx_string(str) { sizeof(str) - 1, (char *) str } #define ngx_str_set(str, text) \ (str)->len = sizeof(text) - 1; (str)->data = (char *) text #define ngx_hash(key, c) ((ngx_uint_t) key * 31 + c) #define ngx_str_null(str) (str)->len = 0; (str)->data = null void build_key_hash_pair(key_hash_pair *h, ngx_str_t api_key, ngx_str_t ip); int main (int argc, char const *argv[]) { ngx_str_t api_key = ngx_string("86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"); ngx_str_t ip = ngx_string("123.123.123.123"); key_hash_pair *pair; pair = malloc(sizeof(key_hash_pair)); build_key_hash_pair(pair, api_key, ip); printf("api_key = %s\n", api_key.data); printf("ip = %s\n", ip.data); printf("pair->key = %s\n", pair->key.data); printf("pair->hash = %u\n", (unsigned int)pair->hash); homecoming 0; } void build_key_hash_pair(key_hash_pair *h, ngx_str_t api_key, ngx_str_t ip) { ngx_str_null(&h->key); char str[56]; memset(str, 0, sizeof(str)); strcat(str, api_key.data); strcat(str, ip.data); ngx_str_set(&h->key, str); ngx_uint_t i; (i = 0; < 56; i++) { h->hash = ngx_hash(&h->hash, h->key.data[i]); } }

here output when printf("hello") within build_key_hash_pair function:

helloapi_key = 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8 ip = 123.123.123.123 pair->key = 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8123.123.123.123 pair->hash = 32509824

and here (bizarre) output when not printf within build_key_hash_pair:

api_key = 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8 ip = 123.123.123.123 pair->key = pair->hash = 32509824

as can see, pair->key has no data. in gdb, if breakpoint right after phone call in main build_key_hash_pair, pair->key contains appropriate data. after first phone call printf, blanked out. memory address stays same, info gone. can tell me in world i'm doing wrong?

this line problem:

ngx_str_set(&h->key, str);

here str local variable, , putting pointer within h->key, returned caller. after build_key_hash_pair returns, pointer no longer valid. when didn't phone call other function, pointer happened still point same value, not can rely on. phone call printf overwrote part of stack.

what need either dynamically allocate string malloc or strdup, or set array within key_hash_pair struct hold key (possible if key same size).

c gdb nginx

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -