Bug 272915 - wcsncmp is not ISO C compliant
Summary: wcsncmp is not ISO C compliant
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: 13.2-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-standards (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-08-03 11:53 UTC by bruno
Modified: 2023-08-06 18:00 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description bruno 2023-08-03 11:53:16 UTC
ISO C 23 § 7.31.4.4.3 together with § 7.31.4.4.(1) and § 7.31.1.(6) specifies that wcsncmp must compare the string elements as wchar_t values, and that all wchar_t values can legitimately occur.

On FreeBSD/x86_64, wchar_t is signed, i.e. equivalent to 'int'. In the test case below, cmp must therefore come out positive, since a[0] > 0 > b[0]. But wcsncmp in FreeBSD compares the wchar_t values as if they were unsigned.

How to reproduce:
================================= foo.c =============================
#include <wchar.h>
int main ()
{
  /* This test fails on glibc < 2.15, musl libc 1.2.3, macOS 12.5,
     FreeBSD 13.2, NetBSD 9.0, OpenBSD 7.2, Solaris 11.4.  */
  wchar_t a[2] = { (wchar_t) 0x76547654, 0 };
  wchar_t b[2] = { (wchar_t) 0x9abc9abc, 0 };
  int cmp = wcsncmp (a, b, 1);
  return (!((wchar_t)-1 < 0 ? cmp > 0 : cmp < 0));
}
=====================================================================
$ cc foo.c
$ ./a.out
$ echo $?
1