I have an application and I am analyzing memory crash dumps of this software.
struct GPS_CONNECTION
{
int sockfd;
std::string sendbuf, recvbuf;
struct sockaddr_in remoteaddr;
};
vector <GPS_CONNECTION> GPSC;
--------------------------------
(cut)
--------------------------------
fd_set master, gps_master, read_fds, gps_read_fds, write_fds, gps_write_fds;
for (;;)
{
/* Clear */
FD_ZERO(&gps_read_fds);
FD_ZERO(&gps_write_fds);
/* read_fds */
gps_read_fds = gps_master;
/* write_fds */
for (int i=0; i < GPSC.size(); i++)
{
if (GPSC[i].sendbuf.empty())
{
continue;
}
FD_SET(GPSC[i].sockfd, &gps_write_fds);
}
/* Timeout struct */
tv.tv_sec = 0;
tv.tv_usec = 0;
/* selectuj write */
if (select(gps_fdmax+1, &gps_read_fds, &gps_write_fds, NULL, &tv) == -1)
{
perror("select");
return 7;
}
--------------------------------
(cut)
--------------------------------
}
GDB crash dump says software is crashed in line:
443 if (GPSC[i].sendbuf.empty())
When I was analyzing the variables I saw this:
(gdb) print i
$1 = -1214807923
I don't understand how this value was overwritten? I don't see any stack overflow issue here, can anyone explain the reason of this crash?
This problem appears recurring - once in 2-days, this is a server working 24/7/365.
After g++ expanding this code that results:
for (int i=0; i < GPSC.size(); i++)
{
if (GPSC[i].sendbuf.empty())
{
continue;
}
__asm__ __volatile__ ("btsl %1,%0" : "=m" (((&gps_write_fds)->fds_bits)[((GPSC[i].sockfd) / (8 * sizeof (__fd_mask)))]) : "r" (((int) (GPSC[i].sockfd)) % (8 * sizeof (__fd_mask))) : "cc","memory");
}
Code will crashing because of multi-threading that i will not consider before, i think that is working in one thread only, my mistake.