c-semantics icon indicating copy to clipboard operation
c-semantics copied to clipboard

strtok_r in string.h can be broken

Open Lycbel opened this issue 7 years ago • 0 comments

this is the minimized input of one probem when using kcc to compile dzen : https://github.com/robm/dzen kcc can compile dzen, however the executable file can't run properly we found it is a problem when using string.h when get the splitted string by the function token1 = strtok_r("a=b;hello", ";", &saveptr1) token1's value is "a=b" when use the function again on the returned value token2 = strtok_r(token1,"=",&saveptr2) error happens here token2 is "a=b" but should be "a" to run the input

wget https://github.com/Lycbel/cs510Files/blob/master/report7/dzen/mini.zip?raw=true -O mini.zip
unzip mini.zip
cd mini
bash run.sh

compile message of kcc:

compile by kcc-----------------------------------------------
main.c: In function ‘fill_ev_table’:
main.c:12:19: warning: implicit declaration of function ‘strtok_r’ [-Wimplicit-function-declaration]
     char *token = strtok_r(str1, ";", &saveptr1);
                   ^
main.c:12:19: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
main.c:15:14: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
     subtoken = strtok_r(token, "=", &saveptr2);
              ^
mytoken onstart=uncollapse,scrollhome
Encountered an unknown error. This may be due to encountering undefined behavior, an unsupported language feature, or a bug in this tool:
      > in strtok_r at /opt/rv-match/c-semantics/profiles/x86_64-linux-gcc-glibc/src/string.c:86:13
        in fill_ev_table at main.c:15:5
        in main at main.c:20:5

    Unknown error (UNK-1)

Unsequenced side effect on scalar object with value computation of same object:
      > in strtok_r at /opt/rv-match/c-semantics/profiles/x86_64-linux-gcc-glibc/src/string.c:87:7
        in fill_ev_table at main.c:15:5
        in main at main.c:20:5

    Undefined behavior (UB-EIO8):
        see C11 section 6.5:2 http://rvdoc.org/C11/6.5
        see C11 section J.2:1 item 35 http://rvdoc.org/C11/J.2
        see CERT-C section EXP30-C http://rvdoc.org/CERT-C/EXP30-C
        see MISRA-C section 8.1:3 http://rvdoc.org/MISRA-C/8.1

subtoken onstart=uncollapse,scrollhome

compile message by gcc

compile by gcc-----------------------------------------------
mytoken onstart=uncollapse,scrollhome
subtoken onstart

Lycbel avatar Dec 13 '18 01:12 Lycbel