Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memset() causing data abort

I'm getting some strange, intermittent, data aborts (< 5% of the time) in some of my code, when calling memset(). The problem is that is usually doesn't happen unless the code is running for a couple days, so it's hard to catch it in the act.

I'm using the following code:

char *msg = (char*)malloc(sizeof(char)*2048);
char *temp = (char*)malloc(sizeof(char)*1024);
memset(msg, 0, 2048);
memset(temp, 0, 1024);
char *tempstr = (char*)malloc(sizeof(char)*128);

sprintf(temp, "%s %s/%s %s%s", EZMPPOST, EZMPTAG, EZMPVER, TYPETXT, EOL);
strcat(msg, temp);

//Add Data
memset(tempstr, '\0', 128);
wcstombs(tempstr, gdevID, wcslen(gdevID));
sprintf(temp, "%s: %s%s", "DeviceID", tempstr, EOL);
strcat(msg, temp);

As you can see, I'm not trying to use memset with a size larger that what's originally allocated with malloc()

Anyone see what might be wrong with this?

like image 281
Adam Haile Avatar asked Nov 27 '22 13:11

Adam Haile


1 Answers

malloc can return NULL if no memory is available. You're not checking for that.

like image 98
Joel Spolsky Avatar answered Dec 10 '22 06:12

Joel Spolsky