Project

General

Profile

Actions

Bug #7344

closed

wcsncasecmp shouldn't take one for the road

Added by James Blachly over 4 years ago. Updated over 4 years ago.

Status:
Closed
Priority:
Normal
Category:
lib - userland libraries
Start date:
2016-08-31
Due date:
% Done:

100%

Estimated time:
2.00 h
Difficulty:
Medium
Tags:
Gerrit CR:

Description

wcsncasecmp(3C) should perform Wide C String N Case CoMParison between two wchar_t * (comparing at most n characters).

However, this fails on illumos in very strange ways which are C versus C++ dependent.

Strangely, defining a const char * fixes_problem = "WHATEVER" anywhere in the program seems to fix the issue ... when compiling with g++. If compiling as C code with gcc, the program still returns wrong value, but now it returns -641 (instead of 641 as below).

Demonstration: demo1 fails ; in demo2 I define a const char * and wcsncasecmp now returns the correct value (zero), but only when compiled as C++

james@smartos-dev:/tmp % cat demo1.cpp
#include<wchar.h>
#include<stdio.h>
#include<stdlib.h>

int main(void) {

    wchar_t s1[1024];
    wchar_t s2[1024];
    size_t n = 3;

    int e = mbstowcs(s1, "SET", 3);
    e = mbstowcs(s2, "SET", 3);

    int ret = wcsncasecmp(s1, s2, n);
    printf("ret: %i\n", ret);

    return 0;
}
james@smartos-dev:/tmp % cat demo2.cpp
#include<wchar.h>
#include<stdio.h>
#include<stdlib.h>

int main(void) {

    const char *fixes_problem = "SET";

    wchar_t s1[1024];
    wchar_t s2[1024];
    size_t n = 3;

    int e = mbstowcs(s1, "SET", 3);
    e = mbstowcs(s2, "SET", 3);

    int ret = wcsncasecmp(s1, s2, n);
    printf("ret: %i\n", ret);

    return 0;
}
james@smartos-dev:/tmp % diff demo1.cpp demo2.cpp
6a7,8
>     const char *fixes_problem = "SET";
>
james@smartos-dev:/tmp % ./demo1
ret: 641
james@smartos-dev:/tmp % ./demo2 
ret: 0

Now demonstrating the difference between C++ and C:

james@smartos-dev:/tmp % mv demo2.cpp demo2.c 
james@smartos-dev:/tmp % g++ demo2.c -o demo2_c++
james@smartos-dev:/tmp % ./demo2_c++
ret: 0
james@smartos-dev:/tmp % gcc demo2.c -o demo2_c 
james@smartos-dev:/tmp % ./demo2_c 
ret: -641
Actions

Also available in: Atom PDF