Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory leak? #5

Open
paulpach opened this issue Jun 15, 2017 · 0 comments
Open

memory leak? #5

paulpach opened this issue Jun 15, 2017 · 0 comments

Comments

@paulpach
Copy link

I could be wrong, but I think this is a memory leak:

    int HttpParser::_parse_desc(char* recv_buf, Request& request) {
        char *p = recv_buf;
        int i = 0;
        enum {METHOD, URL, VER} req_part = METHOD;
        int parse_len = 0;
        while (*p != '\r') {
            if (*p != ' ') {
                if (req_part == METHOD && (*p < 'A' || *p > 'Z')) {
                    return -1;
                }
                parse_len ++;
            } else if (req_part == METHOD) {
                strncpy(request.method, recv_buf, parse_len);
                request.method[parse_len] = '\0';
                parse_len = 0;
                req_part = URL;
            } else if (req_part == URL) {
                char* url = (char*) malloc ((parse_len + 1) * sizeof(char));
                strncpy(url, recv_buf + strlen(request.method) + 1, parse_len);
                url[parse_len] = '\0';
                char*p = strtok(url, "?");
                char*s = strtok(NULL, "?");
                strncpy(request.url, p, strlen(p));
                request.url[strlen(p)] = '\0';
                char* val = NULL;
                char* ky = NULL;
                if (s != NULL) {
                    char *outer_ptr=NULL;  
                    char *inner_ptr=NULL;
                    while ((val = strtok_r(s, "&", &outer_ptr)) != NULL) {
                        s = val;
                        ky = strtok_r(s, "=", &inner_ptr);
                        val = strtok_r(NULL, "=", &inner_ptr);
                        if (ky != NULL && val != NULL) {
                            std::string decode_val = "";
                            decode(std::string(val), decode_val);
                            request.params[std::string(ky)] = decode_val; //decode(std::string(val));
                        }
                        s = NULL;
                    }
                }
                parse_len = 0;
                req_part = VER;
            }
            p++;
            i++;
        }
        if (req_part == URL) {
            if (parse_len != 0) {
                strncpy(request.url, recv_buf + strlen(request.method) + 1, parse_len);
                request.url[parse_len] = '\0';
                parse_len = 0;
            } else {
                return -1;
            }
        }
        if (parse_len == 0) {
            strcpy(request.version, "HTTP/1.1");
        } else {
            strncpy(request.version, recv_buf + strlen(request.method) + strlen(request.url) + 2, parse_len);
            request.version[parse_len] = '\0';
        }
        return i + 2;
    }

See that malloc? there is no corresponding free. Consider using stl::string instead or use auto pointers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant