c - fread(), solaris to unix portability and use of uninitialised values -
valgrind found following error , i, after reading documentation, code , other questions in here couldn't figure out why.
valgrind: first warning
~$ valgrind --vgdb=yes --vgdb-error=0 --read-var-info=yes --leak-check=yes --track-origins=yes debitadmin*
debitadmin ==20720== conditional jump or move depends on uninitialised value(s) ==20720== @ 0x4013bc6: initialise (dbg.c:199) ==20720== 0x4013f5c: ??? (in /opt/ivb/lib/libdbg.so) ==20720== 0x4013917: ??? (in /opt/ivb/lib/libdbg.so) ==20720== 0x5f5ffe: _dl_init (in /lib/ld-2.12.so) ==20720== 0x5e788e: ??? (in /lib/ld-2.12.so) ==20720== uninitialised value created stack allocation ==20720== @ 0x4013a8f: initialise (dbg.c:150) gdb & valgrind: symbols
(gdb) info symbol 0x4013a8f initialise + 5 in section .text of /opt/ivb/lib/libdbg.so (gdb) info symbol 0x4013bc6 initialise + 316 in section .text of /opt/ivb/lib/libdbg.so code: dbg.c
148 void 149 initialise(void) 150 { 151 register int = 0; 152 char buffer[filename_max] = ""; 153 char *program = null; 154 struct sigaction act = {0}; ... ... 195 while ( ! feof(proc_file) && ! ferror(proc_file)) 196 { 197 char ch; 198 fread(&ch,1,1,proc_file); 199 if ( ch != 0 ) 200 fprintf(stderr,"%c",ch); 201 else 202 fprintf(stderr," "); 203 } gdb: backtrace
(gdb) bt #0 0x04013bc6 in initialise () @ dbg.c:199 #1 0x04013f5d in __do_global_ctors_aux () /opt/ivb/lib/libdbg.so #2 0x04013918 in _init () /opt/ivb/lib/libdbg.so #3 0x005f5fff in _dl_init_internal () /lib/ld-linux.so.2 #4 0x005e788f in _dl_start_user () /lib/ld-linux.so.2 gdb: locals
(gdb) info locals ch = 0 '\000' c = 10 extra information
- this code part of library, ported solaris unix
what understood:
valgrind complains
char ch; is not initialised, after
fread(&ch,1,1,proc_file); after checking gdb ch value after above line, have:
ch = 0 '\000' proc_file does't correspond that, can see:
(gdb) print *proc_file $17 = { _flags = -72538984, _io_read_ptr = 0x4352000 "debitadmin", _io_read_end = 0x4352000 "debitadmin", _io_read_base = 0x4352000 "debitadmin", _io_write_base = 0x4352000 "debitadmin", _io_write_ptr = 0x4352000 "debitadmin", _io_write_end = 0x4352000 "debitadmin", _io_buf_base = 0x4352000 "debitadmin", _io_buf_end = 0x4353000 <address 0x4353000 out of bounds>, _io_save_base = 0x0, _io_backup_base = 0x0, _io_save_end = 0x0, _markers = 0x0, _chain = 0x79c580, _fileno = 3, _flags2 = 0, _old_offset = 0, _cur_column = 0, _vtable_offset = 0 '\000', _shortbuf = "", _lock = 0x43dc0c0, _offset = -1, __pad1 = 0x0, __pad2 = 0x43dc0cc, __pad3 = 0x0, __pad4 = 0x0, __pad5 = 0, _mode = -1, _unused2 = '\000' <repeats 39 times> } there no mcve because i'm still not sure problem is, i'm counting on experts me tackle that.
so, question is, why ch empty after fread() assignment? portability issue between solaris , linux? problem or missing something?
update: file has been opened , checked not null.
#elif linux { char name[filename_max]; file *proc_file; sprintf(name,"/proc/%d/cmdline",(int)getpid()); proc_file=fopen (name,"r"); if ( proc_file != null ) { int c; /* read in programs name */ for(c=0; ((fread(&buffer[c],1,1,proc_file)== 1) && (buffer[c]!=0)); c++); /* print out program */ (void)fprintf(stderr,"%s ", buffer); /* , program arguments , not efficient works....*/ fprintf(stderr," "); while ( ! feof(proc_file) && ! ferror(proc_file)) { char ch; test *t; t = fread(&ch,1,1,proc_file); if ( ch != 0 ) fprintf(stderr,"%c",ch); else fprintf(stderr," "); } (void) fprintf(stderr,"\n\n"); } } #endif
q 1. why
chempty after fread() assignment?
(most probably) because fread() failed. see detailed answer below.
q 2.is portability issue between solaris , linux?
no, there possible issue code itself, correctly reported valgrind.
i cannot quite tell below approach solve problem, should consider below points improve code, e.g.,
point 1: check return value of
fread()ensure sucess. iffread()failure, usingchlater invoke read-before-write scenario.chautomatic local variable , not initialized explicitly, invoke undefined behaviour.point 2: read : why
while ( !feof (file) )(almost) wrong?
Comments
Post a Comment