Question

I'm stumped. I'm working on a small data server for a school assignment, that's supposed to be communicating over sockets for this iteration. Most of it's working but I can't quite figure out what valgrind is complaining about, but here's what it all says.

valgrind says;

 Conditional jump or move depends on uninitialised value(s)
 at 0x4C2ABD9: strlen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
 by 0x510F0EF: std::basic_string<char, std::char_traits<char>, std::allocator<char>             >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.17)
 by 0x4078C9: netreq::cread() (netreq.c:120)
 by 0x402585: event(void*) (simpleclient.C:166)

gdb is receiving a SIGPIPE seemingly at the same point in the process.

Here's the function that it is complaining about;

string netreq::cread()
{
    char buf[255];

    if(read(fd,buf, 255) < 0)
         cout << "I cants read dat right, sorry"<<endl;

    return (string)buf; //this is line 120 in netreq.c
}

thoughts? anyone fixed something similar? i've tried quite a few things but no luck yet.

Was it helpful?

Solution

read() does not terminate the array by \0 to indicate the end. You should do it by yourself.

int len;
if ((len = read(fd, buf, 255)) < 0) {
/* ... */
} else {
    buf[len] = '\0';
}

return string(buf);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top