0

I'm currently working on pset6 and can't figure out what I'm doing wrong. I think the problem is the indexes function, because after running GDB I notices the program crashes as it passes that function. I then ran valgrind and it showed a lot of invalid writes of size 1, but i don't understand what the bugs are.

Indexes

char* indexes(const char* path)
{
    char* ppath = malloc(sizeof(strlen(path)) + sizeof("index.") + 1);
    strcpy(ppath, path);
    strcat(ppath, "index.");
    char* php = malloc(sizeof(ppath) + sizeof("php") + 1);
    strcpy(php, ppath);
    strcat(php, "php");

    if (access(php, R_OK) == 0)
    {
        free(ppath);
        return php;
    }

    free(php);
    char* html = malloc(sizeof(ppath) + sizeof("html") + 1);
    strcpy(html, ppath);
    strcat(html, "html");

    if (access(html, R_OK) == 0)
    {
        free(ppath);
        return html;
    }

    free(html);
    free(ppath);
    return NULL;

}

Valgrind Output

==12743== Memcheck, a memory error detector
==12743== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==12743== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==12743== Command: ./server public
==12743== Using /home/ubuntu/workspace/pset6/public for server's root
Listening on port 8080
GET /test/ HTTP/1.1
==12743== Invalid write of size 1
==12743== at 0x4C2E1E0: strcpy (in /usr/lib/valgrind/vgpreload_memcheck- amd64-linux.so)
==12743== by 0x402ACC: indexes (server.c:449)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x5505952 is 0 bytes after a block of size 18 alloc'd
==12743== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402AAA: indexes (server.c:448)
==12743== by 0x401B89: main (server.c:230)
==12743==
==12743== Invalid write of size 1
==12743== at 0x4C2E1F3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402ACC: indexes (server.c:449)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x5505969 is 23 bytes after a block of size 18 alloc'd
==12743== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402AAA: indexes (server.c:448)
==12743== by 0x401B89: main (server.c:230)
==12743==
==12743== Invalid read of size 1
==12743== at 0x4C2DDA4: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402ADD: indexes (server.c:450)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x5505952 is 0 bytes after a block of size 18 alloc'd
==12743== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402AAA: indexes (server.c:448)
==12743== by 0x401B89: main (server.c:230)
==12743==
==12743== Invalid write of size 1
==12743== at 0x4C2DDC0: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402ADD: indexes (server.c:450)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x5505969 is 23 bytes after a block of size 18 alloc'd
==12743== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402AAA: indexes (server.c:448)
==12743== by 0x401B89: main (server.c:230)
==12743==
==12743== Invalid write of size 1
==12743== at 0x4C2DDD3: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402ADD: indexes (server.c:450)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x550596f is not stack'd, malloc'd or (recently) free'd
==12743==
==12743== Invalid write of size 1
==12743== at 0x4C2E1E0: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402B0F: indexes (server.c:452)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x55059af is 0 bytes after a block of size 15 alloc'd
==12743== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402AED: indexes (server.c:451)
==12743== by 0x401B89: main (server.c:230)
==12743==
==12743== Invalid read of size 1
==12743== at 0x4C2E1E4: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402B0F: indexes (server.c:452)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x5505952 is 0 bytes after a block of size 18 alloc'd
==12743== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402AAA: indexes (server.c:448)
==12743== by 0x401B89: main (server.c:230)
==12743==
==12743== Invalid write of size 1
==12743== at 0x4C2E1F3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402B0F: indexes (server.c:452)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x55059cf is not stack'd, malloc'd or (recently) free'd
==12743==
==12743== Invalid read of size 1
==12743== at 0x4C2DDA4: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402B20: indexes (server.c:453)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x55059af is 0 bytes after a block of size 15 alloc'd
==12743== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402AED: indexes (server.c:451)
==12743== by 0x401B89: main (server.c:230)
==12743==
==12743== Invalid write of size 1
==12743== at 0x4C2DDC0: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402B20: indexes (server.c:453)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x55059cf is not stack'd, malloc'd or (recently) free'd
==12743==
==12743== Invalid write of size 1
==12743== at 0x4C2DDD3: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402B20: indexes (server.c:453)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x55059d2 is not stack'd, malloc'd or (recently) free'd
==12743==
==12743== Syscall param access(pathname) points to unaddressable byte(s)
==12743== at 0x52288C7: access (syscall-template.S:81)
==12743== by 0x402B32: indexes (server.c:455)
==12743== by 0x401B89: main (server.c:230)
==12743== Address 0x55059af is 0 bytes after a block of size 15 alloc'd
==12743== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12743== by 0x402AED: indexes (server.c:451)
==12743== by 0x401B89: main (server.c:230)
==12743==

0

Surely your problem is related to the sizeof operator, this operator gives us the size in bytes of a variable, in your case being a char of a byte, for example:

char* html = malloc(sizeof(ppath) + sizeof("html") + 1)

Has three bytes length, while "html" you have 5 bytes (you must take into account the end string character '\ 0'), It is better to change sizeof by strlen, etc, I hope this helps to get started

| improve this answer | |
  • This worked like a charm, thank you! – Pedro Luz Dec 13 '16 at 23:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .