Neeraj Sharma
Neeraj Sharma

Reputation: 1105

Segmentation fault while communicating on sockets: Invalid write of size 1

I'm a beginner at C++. I've written code in interpreted languages before.

I'm writing a client server code in C++ using socket programming to perform some file operations. The client sends the file name to server and server opens that file to perform the operations.

The following snippet shows the procedure for accepting filename from client:

char buf[512];
char *name;
connectionID = accept(socketID, (sockaddr *) NULL, NULL);
cout << "Connection created" << endl;
int bytes = recv(connectionID, buf, sizeof(buf), 0);
buf[bytes] = '\0';
strcpy(name, buf);
cout << name << endl;

This code works randomly. I ported it to another file and it stopped working suddenly. When I try to use GDB for seeing where the segmentation fault occurs, GDB shows either empty lines or any line in the code, seems irrelevant.

>(gdb) where
>#0  0xb74a8979 in ?? ()
>#1  0x00000000 in ?? ()
>(gdb) up
>#1  0x00000000 in ?? ()
>(gdb) down
>#0  0xb74a8979 in ?? ()
>(gdb) down
>Bottom (innermost) frame selected; you cannot go down.

However, valgrind output shows some info, but I still can't figure out what's wrong in here.

>==11855== Invalid write of size 1
>==11855==    at 0x402C6C3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-    linux.so)
>==11855==    by 0x804D38A: main (server.cpp:100)
>==11855==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
>==11855== 
>==11855== 
>==11855== Process terminating with default action of signal 11 (SIGSEGV)
>==11855==  Access not within mapped region at address 0x0
>==11855==    at 0x402C6C3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-l

Valgrind also showed

>==11871== Use of uninitialised value of size 4
>==11871==    at 0x402C6C3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-    linux.so)
>==11871==    by 0x8048DF8: main (in /media/sf_programs/project/a.out)

However, this error does not occur if I initialize name pointer to NULL. Then, only invalid write of size occurrs.

I saw posts for invalid write of size, in those, seg faults occur when pointer is accessed outside of range. But I can't figure out where it's going out of range here.

Do you have any helpful info? It seems the program crashes on receiving string input and storing it from client.

Upvotes: 0

Views: 205

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

char *name;

You made a pointer, great, but you forgot to make it point to anything. Like, for example, a buffer of memory.

Don't obsess over the specific valgrind diagnostic here; they're all on the same topic: you're attempting to use totally random memory that isn't yours.

This is true whether you initialise name to NULL or leave it uninitialised or feed it cream cheese. Until you assign it the location of valid memory that you own, you might as well sing the Macarena on the deck of the Titantic cos this ship might float around a bit for a few scenes (just enough time for some dramatic cinema) but, in the long term, it ain't gonna sail no more!

Upvotes: 1

Related Questions