Daniel Berger
Posts: 1383
Nickname: djberg96
Registered: Sep, 2004
|
Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
|
|
|
|
When documentation lies
|
Posted: Mar 25, 2005 5:11 PM
|
|
|
This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
|
Original Post: When documentation lies
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
|
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...
|
|
I hate it when the documentation is just WRONG. Take the getpwuid_r() function for example. When built with -D_POSIX_PTHREADS_SEMANTICS, the getpwuid_r() function is supposed to return 0 if there was no error. However, as far as I can tell the damned thing always returns 0 on my Solaris box.
Here's an example:
/* Build with gcc -Wall -D_POSIX_PTHREAD_SEMANTICS */
#include <stdio.h>>
#include <pwd.h>
#include <errno.h>
#include <string.h>
#ifdef _SC_GETPW_R_SIZE_MAX
#define USER_BUF_SIZE (sysconf(_SC_GETPW_R_SIZE_MAX))
#else
#define USER_BUF_SIZE 1024
#endif
int main(){
char buf[USER_BUF_SIZE];
struct passwd pwd;
struct passwd* pwdbuf;
uid_t uid = 99999999; /* bogus */
if(getpwuid_r(uid, &pwd, buf, sizeof(buf), &pwdbuf) != 0){
printf("Error: %s\n", strerror(errno));
return -1;
}
printf("Name is: %s\n",pwdbuf->pw_name);
return 0;
}
And yet, AND YET, everything seems ok. It returns 0, and errno is not set but it segfaults when you get to the printf. WTF IS GOING ON?! Naturally I discovered this about 5 minutes after I released sys-admin.
That should have raised an error, but it doesn't. Why? I'm not sure. Maybe it's a bug in the Solaris POSIX implementation. I suppose I should post to comp.unix.solaris and see what the issue is.
Fortunately, there's an easy solution - just do a null check against pwdbuf. The only problem with that is that you only know that it failed. You won't know why it failed because errno isn't set.
FUDGE!
Read: When documentation lies
|
|