code_junkie

memset() causing data abort

Nov 10th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. char *msg = (char*)malloc(sizeof(char)*2048);
  2. char *temp = (char*)malloc(sizeof(char)*1024);
  3. memset(msg, 0, 2048);
  4. memset(temp, 0, 1024);
  5. char *tempstr = (char*)malloc(sizeof(char)*128);
  6.  
  7. sprintf(temp, "%s %s/%s %s%s", EZMPPOST, EZMPTAG, EZMPVER, TYPETXT, EOL);
  8. strcat(msg, temp);
  9.  
  10. //Add Data
  11. memset(tempstr, '', 128);
  12. wcstombs(tempstr, gdevID, wcslen(gdevID));
  13. sprintf(temp, "%s: %s%s", "DeviceID", tempstr, EOL);
  14. strcat(msg, temp);
  15.  
  16. // sizeof(char) is 1 by definition. This memory does not require zero
  17. // initialisation. If it did, I'd use calloc.
  18. const int max_msg = 2048;
  19. char *msg = (char*)malloc(max_msg);
  20. if(!msg)
  21. {
  22. // Allocaton failure
  23. return;
  24. }
  25. // Use snprintf instead of sprintf to avoid buffer overruns
  26. // we write directly to msg, instead of using a temporary buffer and then calling
  27. // strcat. This saves CPU time, saves the temporary buffer, and removes the need
  28. // to zero initialise msg.
  29. snprintf(msg, max_msg, "%s %s/%s %s%s", EZMPPOST, EZMPTAG, EZMPVER, TYPETXT, EOL);
  30.  
  31. //Add Data
  32. size_t len = wcslen(gdevID);
  33. // No need to zero init this
  34. char* temp = (char*)malloc(len);
  35. if(!temp)
  36. {
  37. free(msg);
  38. return;
  39. }
  40. wcstombs(temp, gdevID, len);
  41. // No need to use a temporary buffer - just append directly to the msg, protecting
  42. // against buffer overruns.
  43. snprintf(msg + strlen(msg),
  44. max_msg - strlen(msg), "%s: %s%s", "DeviceID", temp, EOL);
  45. free(temp);
Add Comment
Please, Sign In to add comment