0
votes

I have this context :

char* bname(char const *mypath) {    //basename
    char* bnm = (char*) malloc((strlen(mypath)+1)*sizeof(char));
    char lim='/';
    char* z = strrchr(mypath, lim);
    if (z) {
        strcpy(bnm, z + 1);
    } else {
        strcpy(bnm,mypath);
    }
    return bnm;
}

void doX(int sockfd, char* filename) {
    if (send(sockfd, filename, 1024, 0) == -1) {
        // exit;
    }

    /*
    No error with this one:
    if (send(sockfd, "test", 1024, 0) == -1) {
        // exit
    }
    */
}

Which is called exclusively in main like that :

// argv[3] = test.jpg
char* fname= bname(argv[3]);
doX(socketd, fname);
free(fname);

Compilation : gcc -Wall -Werror -pedantic -std=c99 -ggdb3

Valgrind : valgrind --leak-check=full --tool=memcheck

Syscall param socketcall.sendto(msg) points to unaddressable byte(s)
==7096==    at 0x4F5EC4D: send (send.c:28)
==7096==    by 0x109197: doX (client.c:94)
==7096==    by 0x1093A0: main (client.c:146)
==7096==  Address 0x522e2f9 is 0 bytes after a block of size 9 alloc'd
==7096==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7096==    by 0x108F38: bname (client.c:43)
==7096==    by 0x109378: main (client.c:145)

I can't seem to find the reason of this warning, it most probably is the send() of doX since if I give it a literal string no warning appears.

Your help is appreciated.

1
send(sockfd, filename, 1024, 0): I believe, filename doesn't provide 1024 bytes. Hence, you access out of bound and valgrind doesn't like this. - Scheff's Cat
Btw. send(sockfd, "test", 1024, 0) is wrong as well. No error with this one is just bad luck. (or luckily bad?) - Scheff's Cat
I think send() wants a 'const void *', not a 'char *'. - GermanNerd
@GermanNerd I don't think that's an issue. If it were it would be a compile time issue - not a runtime issue in valgrind... - Scheff's Cat
@GermanNerd the C standard guarantee that void * can be converted to and back from any object, for this exact reason send() use it. The problem is that outlined by @Scheff. The correct syntax should be send(sockfd, filename, strlen(filename), 0) - Frankie_C

1 Answers

0
votes

change it like

if (send(sockfd, filename, strlen(filename)+1, 0) == -1) {

filename may don't have that much memory (1024 bytes) allocated