1841990 Members
3390 Online
110185 Solutions
New Discussion

For C experts

 
dom kris
Frequent Advisor

For C experts

Hi,

I am having a problem with a small C program (extract from a bigger program).
It compiles nicely but at execution, it core dumps.
I use gcc3.2.2 and HP-UX B.11.00 A to compile it.
This is the program:
"
#include
#include
#include
#include
#include


char * sh_unix_getUIDdir (int level, uid_t uid)
{
struct passwd * tempres;
int status = 0;
uid_t seuid, sruid;
seuid = geteuid();
sruid = getuid();

printf("name: %s\n",tempres->pw_name);
printf("seuid: %i\n",seuid);
printf("sruid: %i\n",sruid);
}


main()
{
char * p;

p = sh_unix_getUIDdir (2,0);
}
"

It coredumps on the "printf("name: %s\n",tempres->pw_name);" statement.
It don't understand why.

When I compile it wit HP Ansi C, it does not coredump but there is no value for "tempres->pw_name"

When I more all the code into the main loop, it works fine.

I am not all familiar with C so this might be normal behaviour in C.
I am however having no problem compiling the same thing with the same gcc version on solaris or tru64 (or HP-UX 10.20)

Any help really appreciated.

Kris
9 REPLIES 9
Roberto Polli
Trusted Contributor

Re: For C experts

did you try to put everything in a different variable?

something like
----
char *goofy;
goofy=tempres->pw_name;

printf ("....",goofy);
----

Look at the idea, not at the code! I may be syntax-wrong ;-)

Peace, r.
dom kris
Frequent Advisor

Re: For C experts

Just ignore this post. I found the error in te program.
Stupid me.

Kd
Mark Grant
Honored Contributor

Re: For C experts

I don't understand why it would work in the main loop.

However, you have declared a pointer to a passwd structure called tempres. However, this pointer doesn't point at anything. Therefore, when you print it it runs across a bit of random memory until it falls over in a nasty heap.

Unless I misread this horribly, you need to make "tempres" point at a real password entry using something like getpwent().

Have a look at "man getpwent"
Never preceed any demonstration with anything more predictive than "watch this"
dom kris
Frequent Advisor

Re: For C experts

That was indeed the error.
A. Clay Stephenson
Acclaimed Contributor

Re: For C experts

I see another glaring error. Your function is supposed to return a char pointer but returns nothing.

I'll also point out another very common error that often works by accident.

char *silly_function(int n)
{
char s[80];
(void) sprintf(s,"Silly %d",n);
return(s);
} /* silly_function */

int main()
{
char *p;

p = sillly_function(100);
(void) printf("Silly: %s\n",p);
return(0);
} /* main */

This will compile and often execute perfectly but crash on other platforms or if changes are made to the code. Why? Rather than telling you why, I'll give you the fix and the why is left as an exercise.

Change the declaration in silly_function from
char s[80];
to
static char s[80];




If it ain't broke, I can fix that.
dom kris
Frequent Advisor

Re: For C experts

Thanks for the info.
However, the code example comes from a bigger program (not written by me) and it does return the correct information.

The problem I have is that the "tempres = getpwuid(uid);" call fails with an error code of 215.
I don't see why and I don't find the info what errno 215 means for that call.
I see no reason why this call would fail.

Mark Grant
Honored Contributor

Re: For C experts

Rather worringly, error 215 seems to be "symbol does not exist in executable". I can't quite see how you manage to get that out of getpwuid(). Does it make sense to you?

Still looking at A.Clay's teaser.
Never preceed any demonstration with anything more predictive than "watch this"
dom kris
Frequent Advisor

Re: For C experts

Not at all, but my knowledge of C is very limited.
That's why I wrote that small program mentioned above but 'unfortunately' it works.

dom kris
Frequent Advisor

Re: For C experts

I found the error.
The binary was compiled to produce a static binary.
When I removed the option, it compiled nicely.

The meaning of the error code gave me a hint.
It is strange however that at link time it did not complain

Kris