'wcsncmp's return is weird

Three environment, run the same code with wcsncmp in it, but the result is different.

ENv 1 mac(arm):

mingmingwanng@hostname TDengine % uname -a
Darwin hostname 20.4.0 Darwin Kernel Version 20.4.0: Thu Apr 22 21:46:41 PDT 2021; root:xnu-7195.101.2~1/RELEASE_ARM64_T8101 arm64

mingmingwanng@hostname TDengine % cc --version
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Env 2 linux docker on arm Arch:

root@efe8ae4d51c6:/home/TDinternal/community# uname -a
Linux efe8ae4d51c6 4.15.0-70-generic #79-Ubuntu SMP Tue Nov 12 10:36:10 UTC 2019 aarch64 GNU/Linux

root@efe8ae4d51c6:/# gcc --version
gcc (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Env 3 linux:

root@573cd1810acf:/home/TDinternal# uname -a
Linux 573cd1810acf 5.11.0-43-generic #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC 2021 x86_64 GNU/Linux

root@573cd1810acf:/home/TDinternal# gcc --version
gcc (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The source code:

#include <wchar.h>
#include <stdio.h>
 
int main(){
    wchar_t w1="试试";
    long long w2 = 0xFFFFFFFF;
    int ret = wcsncmp((wchar_t *)&w2, &w1, 1);
    printf("ret:%d\n", ret);
    return 0;
}

The result:

Env 1: ret: -75956121
Env 2: ret:1
Env 3: ret:-1

I have found why the result is -75956121 in environment 1 form the source code:https://github.com/apple-open-source/macos/blob/master/Libc/string/FreeBSD/wcsncmp.c.

but why the result is so big different in environment 2 and environment 3 ?



Solution 1:[1]

Poorly constructed code

wchar_t w1="??"; takes the address of the string "??" and assigns that to w1.

No reason to expect the same address in different compilations nor executions.

Solution 2:[2]

As the pictures below. wchat_t is unsigned, so 0xFFFFFFFF = 4294967295 in Env 2. where in env 3, wchat_t is signed, so 0xFFFFFFFF = -1.

enter image description here

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ming