This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit dbd5fd2b0d1e4a64b5112a6e931b5932c279d79f
Author: ganjing <[email protected]>
AuthorDate: Mon Nov 23 10:25:58 2026 +0800

    libc/locale: return NULL from setlocale() for an invalid category.
    
    setlocale() accepted any integer as the category and returned "C" for it.
    POSIX requires a failure for a value that is not one of the LC_*
    categories, so check the range from LC_CTYPE to LC_ALL first. Valid
    categories behave as before.
    
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/locale/lib_setlocale.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/libs/libc/locale/lib_setlocale.c b/libs/libc/locale/lib_setlocale.c
index 8c91819e564..970e355f14b 100644
--- a/libs/libc/locale/lib_setlocale.c
+++ b/libs/libc/locale/lib_setlocale.c
@@ -50,6 +50,13 @@
 
 FAR char *setlocale(int category, FAR const char *locale)
 {
+  /* An invalid category is an error, whatever the locale name is. */
+
+  if (category < LC_CTYPE || category > LC_ALL)
+    {
+      return NULL;
+    }
+
   if (locale == NULL || strcmp(locale, "POSIX") == 0 ||
       strcmp(locale, "C") == 0 || strcmp(locale, "") == 0)
     {

Reply via email to