Skip to content

LEN_MBCS_PREFIX_LEN() retrives a prefix length incorrectly #173

Description

@komh

Hi/2.

Libc stores a length of the mbcs prefix with macros such as SET_MBCS_PREFIX and LEN_MBCS_PREFIX, but they do not match each other. Especially, LEN_MBCS_PREFIX does not retrive the length correctly.

Here is the test code:

#include <stdio.h>
#include <InnoTekLibc/locale.h>

int main( void )
{
    unsigned char prefixes[ 256 / 4 ] = { 0, };
    int failed;
    int errors = 0;
    int i;

    printf("Testing if a length of MBCS prefix is retrived correctly...\n");

    for( i = 0; i < 256; i++ )
        SET_MBCS_PREFIX( prefixes, i, i % 4 );

    for( i = 0; i < 256; i++ )
    {
        failed = LEN_MBCS_PREFIX( prefixes, i ) != i % 4;
        printf("%s: Prefix length of code point %d = %d(%d)\n",
               failed ? "FAILED" : "PASSED",
               i, LEN_MBCS_PREFIX( prefixes, i ), i % 4 );

        errors += failed;
    }

    return !!errors;
}

And the patch:

0001-Fix-that-LEN_MBCS_PREFIX_LEN-retrives-a-prefix-lengt.patch

From 0d4c7d0292b3d74b28d2c4b636f83bde0616f929 Mon Sep 17 00:00:00 2001
From: KO Myung-Hun <komh78@gmail.com>
Date: Fri, 9 Jan 2026 20:33:07 +0900
Subject: [PATCH] Fix that LEN_MBCS_PREFIX_LEN() retrives a prefix length
 incorrectly

---
 src/emx/include/InnoTekLIBC/locale.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/emx/include/InnoTekLIBC/locale.h b/src/emx/include/InnoTekLIBC/locale.h
index ff33019..444a6e9 100644
--- a/src/emx/include/InnoTekLIBC/locale.h
+++ b/src/emx/include/InnoTekLIBC/locale.h
@@ -308,7 +308,7 @@ extern size_t   __libc_locale_none_wcsnrtombs(char * __restrict, const __wchar_t
     a[((unsigned char)(c)) >> 2] |= (v) << (2 * ((c) & 3))
 
 #define LEN_MBCS_PREFIX(a, c) \
-    ((a[((unsigned char)(c)) >> 2] >> (2 * (((c) & 3) ^ 3))) & 3)
+    ((a[((unsigned char)(c)) >> 2] >> (2 * ((c) & 3))) & 3)
 
 #define IS_MBCS_PREFIX(p, c) \
     (LEN_MBCS_PREFIX((p)->au2MBCSPrefixs, c) != 1)
-- 
2.50.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions