简体   繁体   中英

How can I change the memory address of char*?

I am dealing with a problem that I cannot get abs_path and query arrays filled with a data I am passing to them inside of the function parse. Inside this function logic seems to be correct, I have debugged it and both of the arrays are filled with a correct data. I know that I am not passing a pointer to arrays (char**) in the parameters due to a condition that function's parameters cannot be changed. Any other advice on solving this problem?

#define LimitRequestLine 8190
char abs_path[LimitRequestLine + 1];
char query[LimitRequestLine + 1];

bool parse(const char* line, char* abs_path, char* query)
{
    char* method = "GET ";
    char* valid_http = "HTTP/1.1";
    int index, method_size;
    char abs_path_line[LimitRequestLine + 1];
    char query_line[LimitRequestLine + 1];
    int abs_path_index;

    if(strstr(line, "HTTP/")!=NULL && strstr(line, valid_http) == NULL) {
        error(505);
        return false;
    }

    //make sure that our method is GET
    for(index = 0, method_size = strlen(method); index<method_size; index++) {
        if(line[index] != method[index]) {
            error(405);
            return false; 
        }
    }

    //check if request-target starts with '/'
    if(line[index]!='/') {
            error(501);
            return false; 
    }

    for(abs_path_index = 0; index < strlen(line); index++) {

       //if there is a quotation mark, then we have a query in request-target
       if(line[index] == '?') {
        index++;
        int query_index;

        for(query_index = 0; line[index]!=' '; index++) {

            //check if there is quote mark in query
            if(line[index] == '"') {
                error(400);
                return false; 
            }

            query_line[query_index] = line[index];
            query_index++;
        }

            query_line[query_index] = '\0';
       }

       //assuming that we have not found any '?' mark for query.
       if(strstr(line, "?") == NULL) {
          query_line[0] = '\0'; 
       }

       if(line[index] == ' ') {

           int temp = index;
           index++;

           /*After the space there should be a valid http, if it is not found,
           then there is/are spaces in our request-line which is incorrect*/
           for(int i=0; i<strlen(valid_http); i++) {
               if(line[index] != valid_http[i]) {
                   error(400);
                   return false; 
               }
               index++;
           }

           index = temp;
           break;
       }

       //check if there is quote mark in abs_path
        if(line[index] == '"') {
            error(400);
            return false; 
        }

        abs_path_line[abs_path_index] += line[index];
        abs_path_index++;
    }

    abs_path_line[abs_path_index] += '\0';

    abs_path = abs_path_line;
    abs_path += '\0';
    query = query_line;
    printf("abs path is %s\n", abs_path);
    printf("query is %s\n", query);


    return true;
}

The problem is this:

query = query_line;

char *query means you are passed a pointer. It's just a number like any other number. Think of it this way.

void set_number(int number) {
    number = 6;
}

Do you expect this to do anything? Nope. Same thing with query = query_line .

Instead, query points at a hunk of memory. You need to copy query_line into the memory that query points at and hope there's enough allocated space.

strncpy(query, query_line, LimitRequestLine);

Functions that require the caller to allocate memory are memory problems waiting to happen. Instead of fixing this one I would recommend...

  1. Writing a new function with a better signature, maybe returning a struct.
  2. Implement this old function as a wrapper around the new one.
  3. Deprecating this function.

Note that the query in your function is not the same as the query declared outside the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM