comparison src/debug.c @ 36:6fe121a971c9 stable-0-5-7

valgrind fixes
author carl
date Thu, 09 Aug 2007 15:46:34 -0700
parents b2f247463b83
children ddfb25318812
comparison
equal deleted inserted replaced
35:b2f247463b83 36:6fe121a971c9
256 struct _debug_file_rec_l lfile_rec; 256 struct _debug_file_rec_l lfile_rec;
257 257
258 if (curr_items == 0) return; // no items to write. 258 if (curr_items == 0) return; // no items to write.
259 259
260 index = (int*) xmalloc(index_size); 260 index = (int*) xmalloc(index_size);
261 memset(index, 0, index_size); // valgrind, avoid writing uninitialized data
261 file_pos += index_size; 262 file_pos += index_size;
262 // write the index first, we will re-write it later, but 263 // write the index first, we will re-write it later, but
263 // we want to allocate the space 264 // we want to allocate the space
264 fwrite(index, index_size, 1, debug_fp); 265 fwrite(index, index_size, 1, debug_fp);
265 index[index_ptr++] = curr_items; 266 index[index_ptr++] = curr_items;
266 267
267 item_ptr = item_head; 268 item_ptr = item_head;
268 while (item_ptr) { 269 while (item_ptr) {
269 file_pos = ftell(debug_fp); 270 file_pos = ftell(debug_fp);
270 index[index_ptr++] = file_pos; 271 index[index_ptr++] = file_pos;
271 size = strlen(item_ptr->function)+strlen(item_ptr->file)+ 272 size = strlen(item_ptr->function) +
272 strlen(item_ptr->text) + 3; //for the three \0s 273 strlen(item_ptr->file) +
274 strlen(item_ptr->text) + 3; //for the three \0s
273 if (buf) free(buf); 275 if (buf) free(buf);
274 buf = xmalloc(size+1); 276 buf = xmalloc(size+1);
275 ptr = 0; 277 ptr = 0;
276 funcname=ptr; 278 funcname=ptr;
277 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1; 279 ptr += sprintf(&(buf[ptr]), "%s", item_ptr->function)+1;
385 387
386 void _debug_write_hex(struct _debug_item *item, unsigned char *buf, int size, int col) { 388 void _debug_write_hex(struct _debug_item *item, unsigned char *buf, int size, int col) {
387 struct _debug_file_rec_l lfile_rec; 389 struct _debug_file_rec_l lfile_rec;
388 unsigned char rec_type; 390 unsigned char rec_type;
389 int index_size = 3 * sizeof(int); 391 int index_size = 3 * sizeof(int);
390 int *index = malloc(index_size); 392 int index_pos, file_pos, *index;
391 int index_pos, file_pos;
392 char zero='\0'; 393 char zero='\0';
393 if (!debug_fp) return; // no file 394 if (!debug_fp) return; // no file
395 index = malloc(index_size);
394 index[0] = 1; // only one item in this index run 396 index[0] = 1; // only one item in this index run
397 index[1] = 0; // valgrind, avoid writing uninitialized data
398 index[2] = 0; // ""
395 index_pos = ftell(debug_fp); 399 index_pos = ftell(debug_fp);
396 fwrite(index, index_size, 1, debug_fp); 400 fwrite(index, index_size, 1, debug_fp);
397 index[1] = ftell(debug_fp); 401 index[1] = ftell(debug_fp);
398 402
399 // always use the long 403 // always use the long
400 rec_type = 'L'; 404 rec_type = 'L';
401 fwrite(&rec_type, 1, sizeof(char), debug_fp); 405 fwrite(&rec_type, 1, sizeof(char), debug_fp);
402 lfile_rec.type = item->type;
403 lfile_rec.line = item->line;
404 lfile_rec.funcname = 0; 406 lfile_rec.funcname = 0;
405 lfile_rec.filename = strlen(item->function)+1; 407 lfile_rec.filename = strlen(item->function)+1;
406 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1; 408 lfile_rec.text = lfile_rec.filename+strlen(item->file)+1;
409 lfile_rec.end = 0; // valgrind, avoid writing uninitialized data
410 lfile_rec.line = item->line;
411 lfile_rec.type = item->type;
407 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); 412 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
408 413
409 file_pos = ftell(debug_fp); 414 file_pos = ftell(debug_fp);
410 fwrite(item->function, strlen(item->function)+1, 1, debug_fp); 415 fwrite(item->function, strlen(item->function)+1, 1, debug_fp);
411 fwrite(item->file, strlen(item->file)+1, 1, debug_fp); 416 fwrite(item->file, strlen(item->file)+1, 1, debug_fp);
421 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp); 426 fwrite(&lfile_rec, sizeof(lfile_rec), 1, debug_fp);
422 fseek(debug_fp, 0, SEEK_END); 427 fseek(debug_fp, 0, SEEK_END);
423 } 428 }
424 429
425 430
426 void * xmalloc(size_t size) { 431 void *xmalloc(size_t size) {
427 void *mem = malloc(size); 432 void *mem = malloc(size);
428 if (!mem) { 433 if (!mem) {
429 fprintf(stderr, "xMalloc: Out Of memory [req: %ld]\n", (long)size); 434 fprintf(stderr, "xMalloc: Out Of memory [req: %ld]\n", (long)size);
430 exit(1); 435 exit(1);
431 } 436 }
437 memset(mem, 0, size); // valgrind, some email attachment does not initialize the entire buffer passed to base64 encode
432 return mem; 438 return mem;
433 } 439 }
434 440