char数组损坏 - pset6 cs50x

时间:2015-07-27 15:39:52

标签: arrays corruption

我正在研究pset6,我面临一个无法解释的问题。在我的试用代码中,我验证了请求行,我从请求目标中正确提取了查询,并且我正在尝试连接根和绝对路径。为了测试我的代码,我硬编码了root和request行。我的问题是,当我对一个长根进行硬编码时,在根和绝对路径的串联之后,我提取的查询被破坏并获得一个值,该值是变量路径的一部分,查询存在或为NULL。当硬编码的根很短时,没有问题,查询也没有损坏。变量查询和root在它们之间没有任何连接。任何人都可以帮助我吗?

我的代码是C:

#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
    char* line = "GET /cat.jpg?q=Alice HTTP/1.1\r\n";
    char* root = "/home/jharvard/Dropbox/pset6/public";
    // VALIDATE REQUEST LINE

    // check method is GET

    char* val_req = strchr(line, ' ');
    char test_method[val_req - line + 1];
    char* t_method = "GET";
    strncpy(test_method, line, val_req - line);
    test_method[val_req - line] = '\0';
    int valm = strcmp(test_method, t_method);
    /** checks
        printf("val_req = %s\n", val_req);
        printf("test_method = %s\n", test_method);
        printf("valm = %d\n", valm);
        printf("test_method = %s\n", test_method);
        printf("length of test_method = %d\n", strlen(test_method));
        printf("length of t_method = %d\n", strlen(t_method));*/

    if (valm != 0)
    {
        printf("error(405) \n");

    }
    else
    {
        printf(" correct method\n");
    }



    //check request-target
    char* temp = strchr(val_req + 1, ' ');
    char test_req_tar[temp - val_req];
    strncpy(test_req_tar, val_req + 1, temp - val_req + 1);
    test_req_tar[temp - val_req - 1] = '\0';

    /** checks
    printf("val_req = %s\n", val_req);
    printf("temp = %s\n", temp);
    printf("test_req_tar = %s\n", test_req_tar);*/

    char* find_dot = strchr(test_req_tar, '.');
    char* find_doubles = strchr(test_req_tar, '"');
    if (test_req_tar[0] != '/')
    {
        printf("error (501)");
    }
    else
    {
        printf(" correct initial slash\n");
    }
    if (find_dot == NULL)
    {
        printf("error (501 dot)\n");
    }
    else
    {
        printf(" correct dot\n");
    }
    if (find_doubles != NULL)
    {
        printf("error (400)\n");
    }
    else
    {
        printf(" correct no doubles\n");
    }

    //check version of HTTP
    char* val_ver = strstr(temp, "HTTP/1.1");
    if (val_ver == NULL)
    {
        printf("error (505)\n");  
    }
    else
    {
        printf(" correct HTTP-version\n");
    }


    // EXTRACT QUERY FROM REQUEST-TARGET

    char* temp1 = strchr(test_req_tar, '?');
    int len = 1;
    if (temp1 != NULL)
    {
        len = strlen(temp1);

    }
   char query[len];
   if (temp1 == NULL)
   {
      strcpy(query, "\0");

   }
   else
   {
        strcpy(query, temp1 + 1);
   }

    /* ELEGXOI


    printf("len = %d\n", len);
    printf("size of query %d\n", sizeof(query));  
    printf("query = %s\n", query);
    printf("temp1 = %s\n", temp1);
    printf("test_req_tar = %s\n", test_req_tar);*/

    // CONCATENATE ROOT AND ABSOLUTE PATH


    int lt = 0;
    int lt1 = 0;
    char temp2[strlen(test_req_tar)+1];
    strcpy(temp2, test_req_tar);
    printf("\n**** BEFORE CONCATENATIONS ****\n\n");
    printf("query:%s\n", query);
    printf("test_req_tar:%s\n", test_req_tar);
    printf("temp1: %s\n", temp1);
    printf("temp2: %s\n", temp2);

    if (temp1 == NULL)
    {
        lt1 = strlen(temp2);
    }
    else
    {
        lt1 = temp1 - test_req_tar;
    }
    char path[lt], absol_path[lt1+1];
    strncpy(absol_path, temp2, lt1);
    absol_path[lt1] = '\0';

    lt = strlen(root) + strlen(absol_path);
    strcpy(path, root);
    strcat(path, absol_path);
    printf("\n**** AFTER CONCATENATIONS ****\n\n");
    printf("path: %s\n", path);
    printf("query: %s\n", query);
    printf("test_req_tar: %s\n", test_req_tar);
    printf("temp1: %s\n", temp1);
    printf("temp2: %s\n\n", temp2);


}

0 个答案:

没有答案