'How to copy portion of char buffer into char[]?
I'm trying to read the process name from /proc/[pid]/stat and that is mostly successful. Yet valgrind complains about something, and the process crashes after some time. My code comes from procps-ng with a few modifications. What I've attempted to do is read the file into a buffer, then find a pointer to the start and end of the process name in that buffer. After that strncpy copies the selected section into the destination buffer and then I add null to the end.
Is this the wrong approach?
The relevant bit of code is here:
rc = file2str(path,"stat", &ub);
if (rc <= 0) return rc;
ub.buf = strchr(ub.buf, '(');
if (!ub.buf) return 0;
ub.buf++;
tmp = strrchr(ub.buf, ')');
if (!tmp || !tmp[1]) return 0;
strncpy(name, ub.buf, (tmp - ub.buf));
name[tmp - ub.buf + 1] = 0;
return (tmp - ub.buf);
The same file2str function is called from another place where it works reliably. I've tried to invoke it in the same way, so I am at a loss for why it fails here.
Valgrind says:
Syscall param read(buf) points to unaddressable byte(s)
at 0x4C4A912: read (read.c:26)
by 0x10D2F9: read (unistd.h:47)
by 0x10D2F9: file2str (procs.c:98)
by 0x10D6B0: stat2name (procs.c:159)
by 0x10DEAA: walk_and_draw (pidgrid.c:313)
by 0x4C55D72: trecurse_r (tsearch.c:739)
by 0x4C55D62: trecurse_r (tsearch.c:736)
by 0x4C55D62: trecurse_r (tsearch.c:736)
by 0x4C55D62: trecurse_r (tsearch.c:736)
by 0x4C55D62: trecurse_r (tsearch.c:736)
by 0x4C55D72: trecurse_r (tsearch.c:739)
by 0x4C55D72: trecurse_r (tsearch.c:739)
by 0x4C56B92: trecurse_r (tsearch.c:736)
by 0x4C56B92: twalk_r (tsearch.c:753)
Full code listing can be found here: https://github.com/robbieh/xscreensaver-pidgrid
edit #1
The var name gets filled. In this case with "Isolated Web Co". That gets written into text which contains "PID: 1737571 UID: 1000 RSS: 83241 VSIZE: 3037233152 STATE: S OOMSCORE: 670 -- Isolated Web Co".
Here's what I get out of GDB:
#0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737346060608) at pthread_kill.c:44
#1 __pthread_kill_internal (signo=6, threadid=140737346060608) at pthread_kill.c:80
#2 __GI___pthread_kill (threadid=140737346060608, signo=signo@entry=6) at pthread_kill.c:91
#3 0x00007ffff7b03476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4 0x00007ffff7ae97b7 in __GI_abort () at abort.c:79
#5 0x00007ffff7b4a5e6 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff7c9c13d "%s\n") at ../sysdeps/posix/libc_fatal.c:155
#6 0x00007ffff7b61adc in malloc_printerr (str=str@entry=0x7ffff7c99e4b "realloc(): invalid old size") at malloc.c:5543
#7 0x00007ffff7b65534 in _int_realloc (av=av@entry=0x7ffff7cd5c60 <main_arena>, oldp=oldp@entry=0x555555cd1680, oldsize=<optimized out>, nb=nb@entry=1040) at malloc.c:4718
#8 0x00007ffff7b66149 in __GI___libc_realloc (oldmem=0x555555cd1690, bytes=1024) at malloc.c:3372
#9 0x00007ffff79c565f in ?? () from /lib/x86_64-linux-gnu/libfreetype.so.6
#10 0x00007ffff79c56be in ?? () from /lib/x86_64-linux-gnu/libfreetype.so.6
#11 0x00007ffff79c585c in ?? () from /lib/x86_64-linux-gnu/libfreetype.so.6
#12 0x00007ffff79d370e in ?? () from /lib/x86_64-linux-gnu/libfreetype.so.6
#13 0x00007ffff79da544 in ?? () from /lib/x86_64-linux-gnu/libfreetype.so.6
#14 0x00007ffff79dff6f in ?? () from /lib/x86_64-linux-gnu/libfreetype.so.6
#15 0x00007ffff79c4c88 in FT_Load_Glyph () from /lib/x86_64-linux-gnu/libfreetype.so.6
#16 0x00007ffff7a1371b in ?? () from /lib/x86_64-linux-gnu/libfreetype.so.6
#17 0x00007ffff79c5161 in FT_Load_Glyph () from /lib/x86_64-linux-gnu/libfreetype.so.6
#18 0x00007ffff7f941aa in XftFontLoadGlyphs () from /lib/x86_64-linux-gnu/libXft.so.2
#19 0x00007ffff7f990f3 in XftGlyphRender () from /lib/x86_64-linux-gnu/libXft.so.2
#20 0x00007ffff7f99333 in XftDrawGlyphs () from /lib/x86_64-linux-gnu/libXft.so.2
#21 0x00007ffff7f9994b in XftDrawStringUtf8 () from /lib/x86_64-linux-gnu/libXft.so.2
#22 0x0000555555559f97 in walk_and_draw (what=<optimized out>, which=<optimized out>, closure=0x555555592040) at pidgrid.c:326
#23 0x00007ffff7bdfd73 in trecurse_r (vroot=0x555555bbb790, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:739
#24 0x00007ffff7bdfd73 in trecurse_r (vroot=0x555555bb5fb0, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:739
#25 0x00007ffff7bdfd73 in trecurse_r (vroot=0x555555baaff0, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:739
#26 0x00007ffff7bdfd63 in trecurse_r (vroot=0x555555bc0f70, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:736
#27 0x00007ffff7bdfd63 in trecurse_r (vroot=0x555555bece70, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:736
#28 0x00007ffff7bdfd73 in trecurse_r (vroot=0x555555b95070, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:739
#29 0x00007ffff7bdfd73 in trecurse_r (vroot=0x555555ae5470, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:739
#30 0x00007ffff7bdfd73 in trecurse_r (vroot=0x555555a35870, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:739
#31 0x00007ffff7be0b83 in trecurse_r (closure=0x555555592040, action=0x5555555597b0 <walk_and_draw>, vroot=0x5555555a9b60) at tsearch.c:739
#32 __GI___twalk_r (vroot=0x5555555a9b60, action=0x5555555597b0 <walk_and_draw>, closure=0x555555592040) at tsearch.c:753
#33 0x000055555555a35b in pidgrid_draw (dpy=<optimized out>, window=<optimized out>, closure=0x555555592040) at pidgrid.c:616
#34 0x00005555555586ef in run_screenhack_table (ft=<optimized out>, window2=<optimized out>, window=<optimized out>, dpy=<optimized out>) at screenhack.c:593
#35 main (argc=<optimized out>, argv=<optimized out>) at screenhack.c:996
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
