Bug 10503

Summary: setlocale returns a valid locale and sets errno to ENOENT
Product: glibc Reporter: Pedro Izecksohn <Pedro.Izecksohn>
Component: localedataAssignee: GNU C Library Locale Maintainers <libc-locales>
Status: RESOLVED INVALID    
Severity: normal CC: glibc-bugs
Priority: P2 Flags: fweimer: security-
Version: 2.9   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Pedro Izecksohn 2009-08-10 05:35:29 UTC
If $LANG is any available locale other than C or POSIX,

If /usr/lib/locale/locale-archive does not exist setlocale is setting errno to
ENOENT.

If locale data are stored in /usr/lib/locale/<locale_name>/LC_* files, then
/usr/lib/locale/locale-archive is not needed and errno must not be set.

Try to compile and run the code below:

// slt.c

#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
  char *c = setlocale (LC_NUMERIC, "");
  fprintf (stderr, "errno = %s\n", strerror (errno));
  fprintf (stderr, "setlocale (LC_NUMERIC, \"\") returned %s\n", c);
  return 0;
}

// The C code ended.

Example:

pedro@ubuntu:~/programming/c/setlocale problem$ ./slt.Ubuntu64
errno = Success
setlocale (LC_NUMERIC, "") returned pt_BR.utf8
pedro@ubuntu:~/programming/c/setlocale problem$ sudo mv
/usr/lib/locale/locale-archive /usr/lib/locale/locale-archive.valid
[sudo] password for pedro: 
pedro@ubuntu:~/programming/c/setlocale problem$ ./slt.Ubuntu64
errno = No such file or directory
setlocale (LC_NUMERIC, "") returned pt_BR.utf8
Comment 1 Ulrich Drepper 2009-10-30 05:26:59 UTC
errno is allowed to be modified except in very few exception cases.  setlocale
is not among them.  The caller must not look at errno unless the call fails.