The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
When documentation lies

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
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
Reply to this message Reply

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...

Advertisement
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

Topic: More Rails On The Shelf, O'Reilly Aboard Previous Topic   Next Topic Topic: 65 Across: Julian Date of Ruby Inception

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use