Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lpthreads and getpwuid=segmentation fault??

239 views
Skip to first unread message

Uwe Ludwig

unread,
Mar 5, 2003, 11:49:55 AM3/5/03
to
Hallo,

If I compile a program with

gcc ..... -lpthreads

(i need threads-support) I wont be able to use getpwuid any more. Each call
of getpwuid will result in a segmentation fault.

Has anybody an idea why?

example:
(here are no threads, but its just an exemple which will result in a
segmantation fault when beeing compiled with gcc -oa.out
example.c -lpthreads)

#include<pwd.h>
#include<unistd.h>

char *a;
char *getuser(void){
char *username;
struct passwd *info;
info= getpwuid(getuid());
username=info->pw_name;
return(username);
}
main()
{
a=getuser();
printf(a);
printf("\n");
}


Fletcher Glenn

unread,
Mar 5, 2003, 12:03:45 PM3/5/03
to

Your example above does not test the return value
from getpwuid. According to the man page, an unsuccessful
return is NULL. If you try to dereference NULL you will
get a SIGSEGV.

--
Fletcher Glenn
email f-g-l...@quest.com (remove the dashes)

Message has been deleted

Kenny McCormack

unread,
Jan 4, 2023, 11:28:22 AM1/4/23
to
In article <e95d3f27-47ad-4ff4...@googlegroups.com>,
stryker2k2 <stryk...@gmail.com> wrote:
>ARISE, OLD DEAD THREAD! Hey, this thread is almost legally allowed to drink
>alcohol in the United States!
>
>Anyways... Uwe Ludwig stumbled upon this and now I am stumbling on this
>as well. Even if you code in the proper checks on the return value, you
>will still get a SEGFAULT. There is something weird with lpthreads and
>getpwuid (and also getlogin) and I don't quite know what it is. But,
>yes... the combo of these two will always cause a SEGFAULT before you
>can even check the return.
>
>Stats:
>- Ubuntu 20.04
>- gcc version 9.4.0
>- x86_64-linux-gnu

Could you post code & build instructions?

Then we might at least have a chance at understanding what you're talking
about, Willis...

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/RoyDeLoon

Paul

unread,
Jan 4, 2023, 5:37:29 PM1/4/23
to
On 1/4/2023 11:28 AM, Kenny McCormack wrote:
> In article <e95d3f27-47ad-4ff4...@googlegroups.com>,
> stryker2k2 <stryk...@gmail.com> wrote:
>> ARISE, OLD DEAD THREAD! Hey, this thread is almost legally allowed to drink
>> alcohol in the United States!
>>
>> Anyways... Uwe Ludwig stumbled upon this and now I am stumbling on this
>> as well. Even if you code in the proper checks on the return value, you
>> will still get a SEGFAULT. There is something weird with lpthreads and
>> getpwuid (and also getlogin) and I don't quite know what it is. But,
>> yes... the combo of these two will always cause a SEGFAULT before you
>> can even check the return.
>>
>> Stats:
>> - Ubuntu 20.04
>> - gcc version 9.4.0
>> - x86_64-linux-gnu
>
> Could you post code & build instructions?
>
> Then we might at least have a chance at understanding what you're talking
> about, Willis...
>

Mar 5, 2003, 11:49:55 AM

https://groups.google.com/g/comp.unix.programmer/c/hs23vL73-y0

Paul

Richard Kettlewell

unread,
Jan 4, 2023, 5:57:43 PM1/4/23
to
The program there doesn’t have “the proper checks on the return value”,
so it’s presumably not what you’re actually running. Also, -lpthreads
does not exist; the library is -lpthread. So we don’t yet have the build
commands you used, either. Obviously, nobody can debug code that you’ve
not shared.

I would suggest using gdb or valgrind to identify where your version
crashes.

--
https://www.greenend.org.uk/rjk/

Ben Bacarisse

unread,
Jan 4, 2023, 5:59:25 PM1/4/23
to
That program, with the undefined behaviour removed, but the curious
verbose style kept, is

#include <pwd.h>
#include <unistd.h>
#include <stdio.h>

char *a;

char *getuser(void)
{
char *username;
struct passwd *info;
info = getpwuid(getuid());
username = info->pw_name;
return username;
}

int main(void)
{
a = getuser();
printf(a);
printf("\n");
}

which compiles and runs fine on my system, with either the -pthread or
-lpthread option. Of course, since threads are not used, the linker may
simply be omitting the code that caused the problem 19 years ago.

The old post refers to -lpthreads (with and s) and my Ubuntu and gcc are
newer.

--
Ben.

Scott Lurndal

unread,
Jan 4, 2023, 6:24:20 PM1/4/23
to
The code will fail if there is no password file entry matching
the current uid.

Ben Bacarisse

unread,
Jan 4, 2023, 6:36:19 PM1/4/23
to
Sure, but if that's what the OP is observing, then I can't see the
connection to the pthread library.

--
Ben.
0 new messages