>Synopsis: Bad return value for getpwnam_r et al >Category: 42 >Environment: System : OpenBSD 5.4 Details : OpenBSD 5.4 (GENERIC.MP) #41: Tue Jul 30 15:30:02 MDT 2013 dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
Architecture: OpenBSD.amd64 Machine : amd64 >Description: POSIX says "[t]he getpwnam_r() function shall return zero on success or if the requested entry was not found and no error has occurred. If an error has occurred, an error number shall be returned to indicate the error." However, OpenBSD returns 0 on success or 1 on failure; it doesn't return an error number. Linux, OS X, Solaris, FreeBSD, and NetBSD all return an error number, and in particular return ERANGE when the provided buffer is too small. Background: I was looping over getpwnam_r so I wouldn't have to use sysconf() to get _SC_GETPW_R_SIZE_MAX. Linux/glibc has an annoying habit of setting some limits to INT_MAX. It doesn't in this case, but I'm not confident they won't do something stupid down the line. >How-To-Repeat: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <pwd.h> int main(void) { struct passwd ent, *found; char *buf = NULL; size_t bufsiz = 1; int error; found = NULL; buf = malloc(bufsiz); while ((error = getpwnam_r("root", &ent, buf, bufsiz, &found))) { fprintf(stderr, "bufsiz:%zu error:%s\n", bufsiz, strerror(error)); bufsiz <<= 1; buf = realloc(buf, bufsiz); found = NULL; } printf("pw_dir: %s\n", ent.pw_dir); printf("pw_shell: %s\n", ent.pw_shell); return 0; } >Fix: No easy fix. The supporting routines _pwhashbyname, _pwhashbyuid, etc in lib/libc/gen/getpwent.c don't propogate error values. dmesg: OpenBSD 5.4 (GENERIC.MP) #41: Tue Jul 30 15:30:02 MDT 2013 dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP real mem = 2130575360 (2031MB) avail mem = 2066194432 (1970MB) mainbus0 at root bios0 at mainbus0 acpi at bios0 not configured mpbios0 at bios0: Intel MP Specification 1.4 cpu0 at mainbus0: apid 0 (boot processor) cpu0: Genuine Intel(R) CPU @ 1.60GHz, 600.10 MHz cpu0: FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,MOVBE,NXE,LONG,LAHF,PERF cpu0: 512KB 64b/line 8-way L2 cache cpu0: smt 0, core 0, package 0 cpu0: apic clock running at 100MHz cpu1 at mainbus0: apid 1 (application processor) cpu1: Genuine Intel(R) CPU @ 1.60GHz, 600.00 MHz cpu1: FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,MOVBE,NXE,LONG,LAHF,PERF cpu1: 512KB 64b/line 8-way L2 cache cpu1: smt 1, core 0, package 0 mpbios0: bus 0 is type PCI mpbios0: bus 64 is type ISA ioapic0 at mainbus0: apid 0 pa 0xfec00000, version 20, 24 pins cpu0: unknown Enhanced SpeedStep CPU, msr 0x0615101c06000615 cpu0: using only highest and lowest power states cpu0: Enhanced SpeedStep 600 MHz: speeds: 1600, 600 MHz pci0 at mainbus0 bus 0 pchb0 at pci0 dev 0 function 0 "Intel E600 Host" rev 0x05 pchb1 at pci0 dev 1 function 0 "Intel E600 Config" rev 0x00 ppb0 at pci0 dev 23 function 0 "Intel E600 PCIE" rev 0x00 pci1 at ppb0 bus 1 ppb1 at pci1 dev 0 function 0 "Intel EG20T PCIE" rev 0x01 pci2 at ppb1 bus 2 "Intel EG20T Packet Hub" rev 0x01 at pci2 dev 0 function 0 not configured "Intel EG20T Ethernet" rev 0x01 at pci2 dev 0 function 1 not configured "Intel EG20T GPIO" rev 0x00 at pci2 dev 0 function 2 not configured ohci0 at pci2 dev 2 function 0 "Intel EG20T USB" rev 0x01: apic 0 int 19, version 1.0, legacy support ohci1 at pci2 dev 2 function 1 "Intel EG20T USB" rev 0x01: apic 0 int 19, version 1.0, legacy support ohci2 at pci2 dev 2 function 2 "Intel EG20T USB" rev 0x01: apic 0 int 19, version 1.0, legacy support ehci0 at pci2 dev 2 function 3 "Intel EG20T USB" rev 0x01: apic 0 int 19 usb0 at ehci0: USB revision 2.0 uhub0 at usb0 "Intel EHCI root hub" rev 2.00/1.00 addr 1 "Intel EG20T USB Client" rev 0x01 at pci2 dev 2 function 4 not configured sdhc0 at pci2 dev 4 function 0 "Intel EG20T SDIO" rev 0x01: apic 0 int 18 sdmmc0 at sdhc0 sdhc1 at pci2 dev 4 function 1 "Intel EG20T SDIO" rev 0x01: apic 0 int 18 sdmmc1 at sdhc1 ahci0 at pci2 dev 6 function 0 "Intel EG20T AHCI" rev 0x01: msi, AHCI 1.1 scsibus0 at ahci0: 32 targets sd0 at scsibus0 targ 0 lun 0: <ATA, INTEL SSDSA2VP02, 9CV1> SCSI3 0/direct fixed naa.5001517bb2898ff1 sd0: 19087MB, 512 bytes/sector, 39091248 sectors, thin sd1 at scsibus0 targ 1 lun 0: <ATA, INTEL SSDSA2VP02, 9CV1> SCSI3 0/direct fixed naa.5001517bb28994d3 sd1: 19087MB, 512 bytes/sector, 39091248 sectors, thin ohci3 at pci2 dev 8 function 0 "Intel EG20T USB" rev 0x01: apic 0 int 16, version 1.0, legacy support ohci4 at pci2 dev 8 function 1 "Intel EG20T USB" rev 0x01: apic 0 int 16, version 1.0, legacy support ohci5 at pci2 dev 8 function 2 "Intel EG20T USB" rev 0x01: apic 0 int 16, version 1.0, legacy support ehci1 at pci2 dev 8 function 3 "Intel EG20T USB" rev 0x01: apic 0 int 16 usb1 at ehci1: USB revision 2.0 uhub1 at usb1 "Intel EHCI root hub" rev 2.00/1.00 addr 1 "Intel EG20T DMA" rev 0x00 at pci2 dev 10 function 0 not configured puc0 at pci2 dev 10 function 1 "Intel EG20T Serial" rev 0x00: ports: 1 com com4 at puc0 port 0 apic 0 int 19: ti16750, 64 byte fifo puc1 at pci2 dev 10 function 2 "Intel EG20T Serial" rev 0x00: ports: 1 com com5 at puc1 port 0 apic 0 int 19: ti16750, 64 byte fifo puc2 at pci2 dev 10 function 3 "Intel EG20T Serial" rev 0x00: ports: 1 com com6 at puc2 port 0 apic 0 int 19: ti16750, 64 byte fifo puc3 at pci2 dev 10 function 4 "Intel EG20T Serial" rev 0x00: ports: 1 com com7 at puc3 port 0 apic 0 int 19: ti16750, 64 byte fifo "Intel EG20T DMA" rev 0x00 at pci2 dev 12 function 0 not configured "Intel EG20T SPI" rev 0x00 at pci2 dev 12 function 1 not configured "Intel EG20T I2C" rev 0x00 at pci2 dev 12 function 2 not configured "Intel EG20T CAN" rev 0x00 at pci2 dev 12 function 3 not configured "Intel EG20T 1588" rev 0x01 at pci2 dev 12 function 4 not configured usb2 at ohci0: USB revision 1.0 uhub2 at usb2 "Intel OHCI root hub" rev 1.00/1.00 addr 1 usb3 at ohci1: USB revision 1.0 uhub3 at usb3 "Intel OHCI root hub" rev 1.00/1.00 addr 1 usb4 at ohci2: USB revision 1.0 uhub4 at usb4 "Intel OHCI root hub" rev 1.00/1.00 addr 1 usb5 at ohci3: USB revision 1.0 uhub5 at usb5 "Intel OHCI root hub" rev 1.00/1.00 addr 1 usb6 at ohci4: USB revision 1.0 uhub6 at usb6 "Intel OHCI root hub" rev 1.00/1.00 addr 1 usb7 at ohci5: USB revision 1.0 uhub7 at usb7 "Intel OHCI root hub" rev 1.00/1.00 addr 1 ppb2 at pci0 dev 24 function 0 "Intel E600 PCIE" rev 0x00 pci3 at ppb2 bus 3 ppb3 at pci3 dev 0 function 0 "IDT 89HPES4T4" rev 0x0e pci4 at ppb3 bus 4 ppb4 at pci4 dev 2 function 0 "IDT 89HPES4T4" rev 0x0e pci5 at ppb4 bus 5 em0 at pci5 dev 0 function 0 "Intel 82574L" rev 0x00: msi, address 00:00:24:cf:08:38 ppb5 at pci4 dev 3 function 0 "IDT 89HPES4T4" rev 0x0e pci6 at ppb5 bus 6 em1 at pci6 dev 0 function 0 "Intel 82574L" rev 0x00: msi, address 00:00:24:cf:08:39 ppb6 at pci4 dev 4 function 0 "IDT 89HPES4T4" rev 0x0e pci7 at ppb6 bus 7 ppb7 at pci0 dev 25 function 0 "Intel E600 PCIE" rev 0x00 pci8 at ppb7 bus 8 ppb8 at pci8 dev 0 function 0 "IDT 89HPES4T4" rev 0x0e pci9 at ppb8 bus 9 ppb9 at pci9 dev 2 function 0 "IDT 89HPES4T4" rev 0x0e pci10 at ppb9 bus 10 em2 at pci10 dev 0 function 0 "Intel 82574L" rev 0x00: msi, address 00:00:24:cf:08:3a ppb10 at pci9 dev 3 function 0 "IDT 89HPES4T4" rev 0x0e pci11 at ppb10 bus 11 em3 at pci11 dev 0 function 0 "Intel 82574L" rev 0x00: msi, address 00:00:24:cf:08:3b ppb11 at pci9 dev 4 function 0 "IDT 89HPES4T4" rev 0x0e pci12 at ppb11 bus 12 ppb12 at pci0 dev 26 function 0 "Intel E600 PCIE" rev 0x00 pci13 at ppb12 bus 13 vendor "Oxford", unknown product 0xc208 (class communications subclass serial, rev 0x00) at pci13 dev 0 function 0 not configured tcpcib0 at pci0 dev 31 function 0 "Intel E600 LPC" rev 0x00: 14318179 Hz timer, watchdog isa0 at tcpcib0 isadma0 at isa0 com0 at isa0 port 0x3f8/8 irq 4: ti16750, 64 byte fifo com0: console pcppi0 at isa0 port 0x61 spkr0 at pcppi0 mtrr: Pentium Pro MTRR support vscsi0 at root scsibus1 at vscsi0: 256 targets softraid0 at root scsibus2 at softraid0: 256 targets root on sd0a (a5c639a44063ad4f.a) swap on sd0b dump on sd0b usbdevs: Controller /dev/usb0: addr 1: high speed, self powered, config 1, EHCI root hub(0x0000), Intel(0x8086), rev 1.00 port 1 powered port 2 powered port 3 powered Controller /dev/usb1: addr 1: high speed, self powered, config 1, EHCI root hub(0x0000), Intel(0x8086), rev 1.00 port 1 powered port 2 powered port 3 powered Controller /dev/usb2: addr 1: full speed, self powered, config 1, OHCI root hub(0x0000), Intel(0x8086), rev 1.00 port 1 powered Controller /dev/usb3: addr 1: full speed, self powered, config 1, OHCI root hub(0x0000), Intel(0x8086), rev 1.00 port 1 powered Controller /dev/usb4: addr 1: full speed, self powered, config 1, OHCI root hub(0x0000), Intel(0x8086), rev 1.00 port 1 powered Controller /dev/usb5: addr 1: full speed, self powered, config 1, OHCI root hub(0x0000), Intel(0x8086), rev 1.00 port 1 powered Controller /dev/usb6: addr 1: full speed, self powered, config 1, OHCI root hub(0x0000), Intel(0x8086), rev 1.00 port 1 powered Controller /dev/usb7: addr 1: full speed, self powered, config 1, OHCI root hub(0x0000), Intel(0x8086), rev 1.00 port 1 powered