Bug 14247 - newlocale doesn't always set errno upon failure return
Summary: newlocale doesn't always set errno upon failure return
Status: ASSIGNED
Alias: None
Product: glibc
Classification: Unclassified
Component: locale (show other bugs)
Version: 2.15
: P2 normal
Target Milestone: ---
Assignee: Carlos O'Donell
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-15 03:45 UTC by law
Modified: 2024-02-27 15:41 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments
Potential fix (375 bytes, patch)
2012-06-15 03:45 UTC, law
Details | Diff
0001-locale-Handle-loading-a-missing-locale-twice-Bug-142.patch (2.22 KB, patch)
2023-12-01 17:39 UTC, Carlos O'Donell
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description law 2012-06-15 03:45:50 UTC
Created attachment 6449 [details]
Potential fix

newlocale() neglects to set errno to an appropriate value when failing, if it has already been asked about the same incorrect locale name.

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

int
main(int argc, char **argv)
{
  locale_t loc;

  setlocale(LC_ALL, "C");

  errno = 0;
  loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK,
		  "nb_no.utf8",
		  0);
  printf("result = %p errno = %d\n", loc, errno);

  errno = 0;
  loc = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK,
		  "nb_no.utf8",
		  0);
  printf("result = %p errno = %d\n", loc, errno);

  return 0;
}

  
Actual results:
result = (nil) errno = 2
result = (nil) errno = 0

Expected results:
result = (nil) errno = 2
result = (nil) errno = 2     (or at least something other than 0)
Comment 1 kristian.spangsege@gmail.com 2021-05-23 17:43:47 UTC
This bug is still present in GLIBC 2.31
Comment 2 Carlos O'Donell 2023-12-01 17:12:58 UTC
The fix is incomplete since when multiple paths are part of the lookup the test case still fails.
Comment 3 Carlos O'Donell 2023-12-01 17:39:36 UTC
Created attachment 15233 [details]
0001-locale-Handle-loading-a-missing-locale-twice-Bug-142.patch

Fixes both locations that need fixing.

Adds a test case.