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:
ngx_str_set(&h->key, str);
Here str
is a local variable, and you are putting a pointer to it inside h->key
, which will be returned to the caller. After build_key_hash_pair
returns, 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 to printf
overwrote that part of the stack.
What you need is either to dynamically allocate the string with malloc
or strdup
, or put an array inside the key_hash_pair
struct to hold the key (possible if the key is always the same size).
build_key_hash_pair
uses stack-based array str
to populate the data
field in the key of h
. When you exit from the function, that pointer is no longer valid since str
goes out of scope.
Your results could be anything from apparently correct operation to a program failure. printf
in the function will work, but definitely not if called afterwards. ngx_str_set
needs to allocate memory and copy the text
string into it (to be freed later of course).
I would replace all those macros with functions or inline code, personally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With