I’m writing an nginx module in C and am having some super bizarre results. I’ve extracted a function from my module to test its output as well as the relevant nginx type/macro definitions.
I’m building a struct in my build_key_hash_pair function, then doing a printf() on the contents in main. When I printf the data inside the inner function, main‘s output is valid. When I remove the printf inside the inner function, main prints an empty string. This is confusing because after the function call to build_key_hash_pair I am not operating on the data except to display it. Here is the 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);
return 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;
for (i = 0; i < 56; i++) {
h->hash = ngx_hash(&h->hash, h->key.data[i]);
}
}
Here is the output when I do a printf("hello") inside the build_key_hash_pair function:
helloapi_key = 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8
ip = 123.123.123.123
pair->key = 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8123.123.123.123
pair->hash = 32509824
And here is the (bizarre) output when I do NOT printf inside build_key_hash_pair:
api_key = 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8
ip = 123.123.123.123
pair->key =
pair->hash = 32509824
As you can see, pair->key has no data. In gdb, if I breakpoint right after the call in main to build_key_hash_pair, pair->key contains the appropriate data. But after the first call to printf, it is blanked out. The memory address stays the same, but the data is just gone. Can anyone tell me what in the world I’m doing wrong?
This line is a problem:
Here
stris a local variable, and you are putting a pointer to it insideh->key, which will be returned to the caller. Afterbuild_key_hash_pairreturns, the pointer will no longer be valid. When you didn’t call any other function, the pointer happened to still point to the same value, but this is not something you can rely on. The call toprintfoverwrote that part of the stack.What you need is either to dynamically allocate the string with
mallocorstrdup, or put an array inside thekey_hash_pairstruct to hold the key (possible if the key is always the same size).