bug in strrchr() - always return 0 when you search '\0' char

strrchr() - return 0 when you search '\0' char

:sample test code

void test(void)
{
    char * ptr; 
    char testString[8] = "hello";

    ptr = strrchr( testString, '\0' );

    if(ptr)
    {
        *(ptr - 1) = '~';
    }
    printf("%s\n",testString); 
}

Expected output : hell~     (Tested and works on standard libraries on windows and Linux)

Actual output:hello.  (On KEIL, Not ok)


IDE-Version:
µVision V5.28.0.0
Copyright (C) 2019 ARM Ltd and ARM Germany GmbH. All rights reserved.

Tool Version Numbers:
Toolchain: MDK Professional 5.28 (Flex) Version: 5.28.0.0
Toolchain Path: C:\Keil_MDK528a\ARM\ARMCC\Bin
C Compiler: Armcc.exe V5.06 update 6 (build 750)
Assembler: Armasm.exe V5.06 update 6 (build 750)
Linker/Locator: ArmLink.exe V5.06 update 6 (build 750)
Library Manager: ArmAr.exe V5.06 update 6 (build 750)
Hex Converter: FromElf.exe V5.06 update 6 (build 750)
CPU DLL: SARMCM3.DLL V5.28.0.0
Dialog DLL: TCM.DLL V1.36.2.0
Target DLL: STLink\ST-LINKIII-KEIL_SWO.dll V3.0.6.0
Dialog DLL: TCM.DLL V1.36.2.0

 

  • My guess is that you are using microlib.

    Microlib is not compliant with the C library standard.  Some features are not supported and some are supported with reduced functionality.

    strrchr seems to be implemented in the microlib with reduced functionality.

    If you don't use microlib, strrchr will behave as you expect.  If you do use microlib, strrchr has the issue that it cannot be used to find the end of a string.

More questions in this forum