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.
send(sockfd, filename, 1024, 0): I believe,filenamedoesn't provide 1024 bytes. Hence, you access out of bound and valgrind doesn't like this. - Scheff's Catsend(sockfd, "test", 1024, 0)is wrong as well. No error with this one is just bad luck. (or luckily bad?) - Scheff's Catvoid *can be converted to and back from any object, for this exact reasonsend()use it. The problem is that outlined by @Scheff. The correct syntax should besend(sockfd, filename, strlen(filename), 0)- Frankie_C