Bug 120982 - opendir returns wrong errno when memory allocation fails.
opendir returns wrong errno when memory allocation fails.
Product: Fedora
Classification: Fedora
Component: glibc (Show other bugs)
All Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Brian Brock
Depends On:
  Show dependency treegraph
Reported: 2004-04-15 16:44 EDT by Oleg Drokin
Modified: 2007-11-30 17:10 EST (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2004-04-15 17:34:08 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Oleg Drokin 2004-04-15 16:44:17 EDT
Description of problem:

opendir(3) return incorrect errno value if memory allocation failed.

Version-Release number of selected component (if applicable):
2.3.2-101.4 (also all prior ones that were included into FC1)

How reproducible:

make a __wrap_malloc function that returns zero and call opendir(),
it will return NULL, errno will contain something strange (in my case
EINTR that was there just prior to the call to opendir).
errno should contain ENOMEM I believe.
We came across this problem while using UserModeLinux,
the wrapper malloc used tried to allocate memory with kernel's kmalloc
implementation that is limited to 128k, by default and opendir
tried to allocate a slightly bigger amount, but errno diverted our
attention and we looked elsewhere for the problem until actually
I singlestepped through opendir and found the real reason.
Comment 1 Jakub Jelinek 2004-04-15 16:56:53 EDT
Returns zero and doesn't set errno to ENOMEM?
glibc of course relies on malloc/calloc/realloc setting errno to ENOMEM
if it fails.
Comment 2 Oleg Drokin 2004-04-15 17:24:06 EDT
Hm. Our malloc implementation does not set errno to ENOMEM and
according to malloc man page in FC1:
       For calloc() and malloc(), the value returned is a pointer to
the allo-
       cated  memory,  which  is suitably aligned for any kind of
variable, or
       NULL if the request fails.

and not a word about setting errno. Or is this sort of internal
undocumented feature glibc is relying on and should we actually go
ahead and set the errno in our malloc wrapper?
Comment 3 Jakub Jelinek 2004-04-15 17:34:08 EDT
It is mandated by the standards.
See e.g.

Otherwise, it shall return a null pointer and set errno to indicate the error.
The malloc() function shall fail if: [ENOMEM] Insufficient storage space is available.

Note You need to log in before you can comment on or make changes to this bug.