tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] An interesting bug with clock() in debug mode


From: Stefanos
Subject: [Tinycc-devel] An interesting bug with clock() in debug mode
Date: Wed, 23 Nov 2022 22:09:15 +0200

Greetings everyone,

I hope I find you well.

I was playing with C and discovered an interesting TCC bug by mistake!

Here's the code I used:

```
cat demo.tinyc 
#if 0
set -e;
exec tcc -Wall -std=c11 -x c -run "$0"
#endif

/*
 * Original example taken from
 * https://riptutorial.com/c/example/10946/copying-strings
 * but of course I have expanded it a bit further.
 */
#include <stdio.h>
#include <string.h>
#include <assert.h>

#ifdef DEBUG
#include <time.h>
#endif

int main(void)
{
#ifdef DEBUG
    clock_t begin = clock();
    printf("DEBUG MODE ON (current clock time (begin) is %ld)\n", begin);
#endif

    char dst[24];
    printf("sizeof(dst): %3zu bytes\n", sizeof(dst));
    memset(dst, '\0', sizeof(dst));
    char src[] = "Marvin and Nemo";
    size_t len = strlen(dst);
    printf("strlen(dst): %3zu characters\n", len);

    strcpy(dst, "Clownfish: ");
    assert(len < sizeof(dst) - 1);
    strncat(dst + len, src, sizeof(dst) - len - 1);
    putchar('\n');
    printf("sizeof(dst): %3zu bytes\n", sizeof(dst));
    printf("dst(\"%s\") [%zu characters]\n", dst, strlen(dst));

#ifdef DEBUG
    printf("Current time via clock(): %ld, initial time (begin): %ld\n", 
clock(), begin);
    double timespent = (double)((clock() - begin)) / CLOCKS_PER_SEC;

    printf("Total time: %f secs, %.3f ms, %.2f μs\n",
            timespent, timespent*1000.0, timespent*1000.0*1000.0);
#endif
    return 0;
}

/* vim: set ft=c: */
```

Here is the command and output of GCC:

gcc -x c -Wall -Wextra -Wpedantic -std=c11 -DDEBUG demo.tinyc -o dt && ./dt && 
rm dt
DEBUG MODE ON (current clock time (begin) is 514)
sizeof(dst):  24 bytes
strlen(dst):   0 characters

sizeof(dst):  24 bytes
dst("Clownfish: Marvin and Nemo") [26 characters]
Current time via clock(): 549, initial time (begin): 514
Total time: 0.000037 secs, 0.037 ms, 37.00 μs


Here's also clang's:

clang -x c -Wall -Wextra -Wpedantic -std=c11 -DDEBUG demo.tinyc -o dt && ./dt 
&& rm dt
DEBUG MODE ON (current clock time (begin) is 615)
sizeof(dst):  24 bytes
strlen(dst):   0 characters

sizeof(dst):  24 bytes
dst("Clownfish: Marvin and Nemo") [26 characters]
Current time via clock(): 659, initial time (begin): 615
Total time: 0.000046 secs, 0.046 ms, 46.00 μs

Now here's TCC's output:

tcc -x c -Wall -std=c11 -DDEBUG demo.tinyc -o dt && ./dt && rm dt
DEBUG MODE ON (current clock time (begin) is 521)
sizeof(dst):  24 bytes
strlen(dst):   0 characters

sizeof(dst):  24 bytes
dst("Clownfish: Marvin and Nemo") [26 characters]
Current time via clock(): 557, initial time (begin): 28525
Total time: -0.027966 secs, -27.966 ms, -27966.00 μs

No matter how many times I execute it, initial time (begin) remains 28525.

I have no idea where to look for a possible fix.

Please advice.

Regards,

Stefanos

P.S.: the #if 0 trick is to create a portable way of scripting my code so 
I can test with all C compilers that exist out there; without it, it 
complains about the #! character on first line of code and does not 
let me compile it.

As you can see, I have already demonstrated that way with 3 different 
compilers, whereas in reality I use it as `./demo.tinyc`.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]