mardi 29 avril 2014

segfault on memcpy once location approaches 100KB


Vote count:

0




I'm attempting to "packetize" a large mmap()d file, like so:



//numBytes is based on user input
data = static_cast<char*>(mmap((caddr_t)0, numBytes, PROT_READ, MAP_SHARED, myFile, 0));

int
Sender::Packetize(char* data, int numBytes)
{
int seqNum = 1;
int offset = 0;
size_t totalPacked = 0;
unsigned int length = sizeof(struct sockaddr_in);

bool dataRemaining = true;
while(dataRemaining)
{
//MTU = 1460
size_t payloadSize;
(numBytes > MTU) ? payloadSize = MTU : payloadSize = numBytes;
char* payload = (char*)malloc(payloadSize);

memcpy(payload, data, payloadSize);
Packet pac = {seqNum, 0, payloadSize, payload}; //Basic struct

totalPacked += payloadSize;
cout << "Packed Bytes: " << payloadSize << endl;
cout << "Total Packed: " << totalPacked << endl;

dataMap.insert(pair<int, struct Packet>(seqNum, pac));

if(numBytes > MTU)
{
offset += MTU;
data = &data[offset];
}
else
dataRemaining = false;

numBytes -= MTU;
seqNum++;
}

return 0;
}


I'm working with a 2MB+ file. When I pass in something relatively small (5000), everything appears to work swimmingly. However, if I attempt to pass the entire file (2533431), I get segfaults during the memcpy(). I've noticed that it appears to be an issue at around 100KB:



[.. snip ..]
Packed Bytes: 1460
Total Packed: 99280
Packed Bytes: 1460
Total Packed: 100740
Packed Bytes: 1460
Total Packed: 102200
Segmentation fault (core dumped)


However, if I attempt to go one chunk smaller (100740), I get:



[.. snip ..]
Packed Bytes: 1460
Total Packed: 16060
Packed Bytes: 1460
Total Packed: 17520
Packed Bytes: 1460
Total Packed: 18980
Segmentation fault (core dumped)


Is there some fundamental flaw that I've overlooked, causing my VM to segfault here?



asked 32 secs ago

MrDuk

1,076





Aucun commentaire:

Enregistrer un commentaire