Summary: | ptsname system call is supposed to return a (char*), but it does not. | ||
---|---|---|---|
Product: | Gentoo Linux | Reporter: | Ferris McCormick (RETIRED) <fmccor> |
Component: | [OLD] Core system | Assignee: | Gentoo Toolchain Maintainers <toolchain> |
Status: | RESOLVED INVALID | ||
Severity: | minor | ||
Priority: | Normal | ||
Version: | 2006.0 | ||
Hardware: | AMD64 | ||
OS: | Linux | ||
Whiteboard: | |||
Package list: | Runtime testing required: | --- | |
Attachments: |
Tiny example program extracted from xconsole
Tiny example program extracted from xconsole |
Description
Ferris McCormick (RETIRED)
2006-03-20 12:50:11 UTC
Created attachment 82708 [details]
Tiny example program extracted from xconsole
Compile with my normal CFLAGS, it uses ptsname_r, and works fine. Add -DSEG_FAULT, it compiles as used in xconsole, and SegFaults:
/dev/ptmx is open with descr = 3
fd 3 gets pseudo-terminal pointer aade5990
Segmentation fault
For what it's worth, this gets recorded in dmesg, too, thus:
bug[7991]: segfault at ffffffffaade5990 rip 00002aaaaac2ed00 rsp 00007fffffe167d8 error 4
(where I used
gcc -o bug -DSEG_FAULT -O2 -march=k8 -pipe ptsname-bug.c)
Created attachment 82709 [details]
Tiny example program extracted from xconsole
Compile with my normal CFLAGS, it uses ptsname_r, and works fine. Add -DSEG_FAULT, it compiles as used in xconsole, and SegFaults:
/dev/ptmx is open with descr = 3
fd 3 gets pseudo-terminal pointer aade5990
Segmentation fault
For what it's worth, this gets recorded in dmesg, too, thus:
bug[7991]: segfault at ffffffffaade5990 rip 00002aaaaac2ed00 rsp 00007fffffe167d8 error 4
(where I used
gcc -o bug -DSEG_FAULT -O2 -march=k8 -pipe ptsname-bug.c)
dont ignore warnings, they exist for a reason :P build with -Wall and you should see why your example code is wrong ... (In reply to comment #3) > dont ignore warnings, they exist for a reason :P > > build with -Wall and you should see why your example code is wrong ... > I see the warning. But it's not my code, it's from xconsole, and it conforms to the documentation. I normally presume documentation to be correct, and so the warning indicative of a bug. And for what it's worth, on sparc I get the same warning, but output (in toto) is: ====================================== gcc -o bug -O2 -mcpu=ultrasparc3 -DSEG_FAULT pts*c ptsname-bug.c: In function `main': ptsname-bug.c:26: warning: assignment makes pointer from integer without a cast fmccor@polylepis Packages [12]% ./bug /dev/ptmx is open with descr = 3 fd 3 gets pseudo-terminal pointer 70195668 Pseudo-terminal is /dev/pts/13 ==================================== Which is what man page says should happen. Curious, because in glibc (sysdeps/unix/sysv/linux/ptsname.c):
char *
ptsname (int fd)
{
return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
}
where ptrname_r is a weak alias for __ptsname_r and buffer is declared 'static char buffer[29];' - so really there should be no difference.
> fd 3 gets pseudo-terminal pointer aade5990
> segfault at ffffffffaade5990
suggests a sign problem, but I think that's in your fprintf, which should be:
fprintf(stderr, "fd %d gets pseudo-terminal pointer %p\n", pty, ptc);
What happens if you add a cast to '(char *)' in front of the call to ptsname - shuts GCC up, so perhaps it has an effect.
it works on sparc because sparc is a 32bit host amd64 is a 64bit host implicit prototypes have an int as a return value ... on amd64, an int is a 32bit quantity ... thus your 64bit pointer is truncated to 32bits and everything crashes the bug is in xconsole, not glibc ... and i'm pretty sure xconsole does *not* conform to the documentation as the man page says you need to define at least _XOPEN_SOURCE before including the header file ... or you could be lazy and just define _GNU_SOURCE |