Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:15:31+00:00 2026-05-23T14:15:31+00:00

I’m writing an nginx module in C and am having some super bizarre results.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:15:32+00:00Added an answer on May 23, 2026 at 2:15 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.