Why is it evbuffer_add_printf will only accept static variables and not "dynamic" ones?

StackOverflow https://stackoverflow.com/questions/14549138

  •  05-03-2022
  •  | 
  •  

Question

So far I have gotten my libev code to successfully return a static sting that says "OMP OMP", however when I write a function that returns a "static" string it never seems to work. (Sidenote: the idea is to turn that same function into a dynamic response but just for agile testing purposes I need this to work first). My code for the libev read callback is as the following...

void p2pserver_network_buf_read_callback(struct bufferevent *incoming, void *arg){
    //Define function local variables
    struct evbuffer *evreturn;
    char *req;

    //Begin function local logic
    req = evbuffer_readline(incoming->input);
    if (req == NULL){
        return;    
    }


    char *response;
    parse_json_command(req, response);
    //response = "OMP OMP";
    g_print("PARSED");
    evreturn = evbuffer_new();
    evbuffer_add_printf(evreturn, "%s", response);

    bufferevent_write_buffer(incoming,evreturn);
    evbuffer_free(evreturn);
    free(req);

    g_print("%s", response);
}

The parse_json_command function is as the following...

void parse_json_command(char json_command, char *response){

    //Define Local Variables
    g_print("PARSING");

    response = "YOU KNOW";

    //Print out the recieved message....
    //g_message("%s", json_command);


    /** 
     * TODO: check if the JSON is valid before parsing 
     *          to prevent "Segmentation Defaults" 
     *          and its good sanity checks.
     **/

    //Parse JSON incomming
    /*json_object * jobj = json_tokener_parse(json_command);

    enum json_type type;
    json_object_object_foreach(jobj, key, val){
        g_print("%s\n", key);

        if(g_utf8_collate(key, "cmd") >= 0){
            //Looks like the user has sent a "cmd" (command), lets analyze the "val" (value) of that command to see what the caller/client needs to be attending to...

            //Is the client requesting an "Identity Update" (Pings server: if this is the first time ping, the server and client will exachange keys if the relationship exists the server just accepts the encrypted "ping" packet update)
            type = json_object_get_type(val);
            if(type == json_type_string){
                char* cmd_value;
                cmd_value = json_object_get_string(val);
                //g_print("VALUE:%d\n", g_utf8_collate(cmd_value, "identupdate"));
                if(g_utf8_collate(cmd_value, "identupdate") == 0){
                    //Call "Identity Update Response"
                        //char return_response = p2pserver_json_identupdate_response(json_command);

                }
            }
        }
    }
    */
    return;
}

If you want to see the complete code (only a couple of pages big at the time of this writing) you can go to the source code at the following link: https://github.com/Xenland/P2PCrypt-Server

Thanks for your time and help!

Was it helpful?

Solution

c passes arguments by value, not by reference. You problem is here:

void parse_json_command(char json_command, char *response){
    [...]
    response = "YOU KNOW";
    [...]
}

char *response;
parse_json_command(req, response);

response is a uninitialized pointer to a string. You are assigning a pointer to a static string to the response pointer in the function, but that does not modify response outside of the function, it just changes response within the function. There are different ways to fix this. Probably the easiest one for a quick fix would be to change the function's prototype to return a char * instead of void:

char * parse_json_command(char json_command){
    char *response;
    [...]
    response = "YOU KNOW";
    [...]
    return response;
}

char *response;
response = parse_json_command(req);

Also, the json_command argument should probably be a char * or const char *, not just a single char, if you want to pass more than a single byte there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top