crashappsec / chalk

Chalk allows you to follow code from development, through builds and into production.
https://crashoverride.com/
GNU General Public License v3.0
348 stars 15 forks source link

Build errors with Clang 15+, or GCC 14+ #290

Closed ee7 closed 1 month ago

ee7 commented 4 months ago

Steps to reproduce

Run nimble build with the default C compiler as either:

Expected behavior

Compilation succeeds.

Observed behavior

Compilation errors.

Diagnosis

In these newer compiler versions, some warnings are now errors. See the breaking changes section of the release notes for Clang 16:

  • The -Wimplicit-function-declaration and -Wimplicit-int warnings now default to an error in C99, C11, and C17. As of C2x, support for implicit function declarations and implicit int has been removed, and the warning options will have no effect. Specifying -Wimplicit-int in C89 mode will now issue warnings instead of being a noop.

  • -Wincompatible-function-pointer-types now defaults to an error in all C language modes.

and Clang 15:

  • The -Wint-conversion warning diagnostic for implicit int <-> pointer conversions now defaults to an error in all C language modes.

and the porting guide for GCC 14:

The initial ISO C standard and its 1999 revision removed support for many C language features that were widely known as sources of application bugs due to accidental misuse. For backwards compatibility, GCC 13 and earlier diagnosed use of these features as warnings only. Although these warnings have been enabled by default for many releases, experience shows that these warnings are easily ignored, resulting in difficult to diagnose bugs. In GCC 14, these issues are now reported as errors, and no output file is created, providing clearer feedback to programmers that something is wrong.

Implicit function declarations (-Werror=implicit-function-declaration)

It is no longer possible to call a function that has not been declared. In general, the solution is to include a header file with an appropriate function prototype. Note that GCC will perform further type checks based on the function prototype, which can reveal further type errors that require additional changes.

Using pointers as integers and vice versa (-Werror=int-conversion)

GCC no longer treats integer types and pointer types as equivalent in assignments (including implied assignments of function arguments and return values), and instead fails the compilation with a type error.

Type checking on pointer types (-Werror=incompatible-pointer-types)

GCC no longer allows implicitly casting all pointer types to all other pointer types. This behavior is now restricted to the void * type and its qualified variations.

This ticket is partly for visibility - the errors will be resolved in nimutils. In case someone wants to disable the errors for now, add to your Nim cfg file:

# Disable some errors for Clang 15+ and GCC 14+.
passC = "-Wno-error=int-conversion"
passC = "-Wno-error=implicit-function-declaration"
passC = "-Wno-error=incompatible-pointer-types"

Details

$ git clone --depth 1 https://github.com/crashappsec/chalk /tmp/chalk
$ cd /tmp/chalk
$ git log -1 --oneline
ca217bd (grafted, HEAD -> main, origin/main, origin/HEAD) test(fd_cache): make tests self-contained, and enable in CI (#276)
$ gcc --version | head -n1
gcc (GCC) 14.1.1 20240507
$ nimble build                                                                                                                                                                                                               /tmp/chalk
[...]
/home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/switchboard.c: In function ‘process_status_check’:
/home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/switchboard.c:1317:17: error: implicit declaration of function ‘waitpid’ [-Wimplicit-function-declaration]
 1317 |         switch (waitpid(subproc->pid, &stat_info, flag)) {
      |                 ^~~~~~~
Error: execution of an external compiler program '/home/foo/.local/c0/musl/bin/musl-gcc -c  -w -fmax-errors=3 -pthread -static -O3 -fno-strict-aliasing -fno-ident -fno-math-errno   -I/home/foo/.choosenim/toolchains/nim-2.0.0/lib -I/home/foo/.local/c0/include -I/home/foo/.local/c0/include -I/tmp/chalk/src -o /tmp/nimcache/chalk/d/@m..@s..@s..@shome@sfoo@s.nimble@spkgs2@snimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042@snimutils@sc@sswitchboard.c.o /home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/switchboard.c' failed with exit code: 1

/home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/strcontainer.c: In function ‘c4string_new’:
/home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/strcontainer.c:15:57: error: passing argument 1 of ‘calloc’ makes integer from pointer without a cast [-Wint-conversion]
   15 |     real_str_t *real_obj = calloc(len + sizeof(int64_t) + 1, 1);
      |                                   ~~~~~~~~~~~~~~~~~~~~~~^~~
      |                                                         |
      |                                                         int64_t * {aka long int *}
In file included from /home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/strcontainer.c:3:
/usr/lib/musl/include/stdlib.h:41:15: note: expected ‘size_t’ {aka ‘long unsigned int’} but argument is of type ‘int64_t *’ {aka ‘long int *’}
   41 | void *calloc (size_t, size_t);
      |               ^~~~~~
/home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/strcontainer.c:17:19: error: assignment to ‘int64_t’ {aka ‘long int’} from ‘int64_t *’ {aka ‘long int *’} makes integer from pointer without a cast [-Wint-conversion]
   17 |     real_obj->len = len;
      |                   ^
/home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/strcontainer.c: In function ‘c4string_from_cstr’:
/home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/strcontainer.c:24:36: error: passing argument 1 of ‘c4string_new’ makes pointer from integer without a cast [-Wint-conversion]
   24 |     str_t   *result = c4string_new(l);
      |                                    ^
      |                                    |
      |                                    int64_t {aka long int}
/home/foo/.nimble/pkgs2/nimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042/nimutils/c/strcontainer.c:13:23: note: expected ‘int64_t *’ {aka ‘long int *’} but argument is of type ‘int64_t’ {aka ‘long int’}
   13 | c4string_new(int64_t *len)
      |              ~~~~~~~~~^~~
[...]
In file included from /tmp/nimcache/chalk/d/@m..@s..@s..@shome@sfoo@s.nimble@spkgs2@snimutils-0.2.1-f4d51b91af9417f9105e0d7d7044466e0a01e042@snimutils@smarkdown.nim.c:181:
/home/foo/.local/c0/include/md4c.h: In function ‘md_is_autolink_email’:
/home/foo/.local/c0/include/md4c.h:3470:77: error: passing argument 2 of ‘is_any_of’ from incompatible pointer type [-Wincompatible-pointer-types]
 3470 |     while(off < max_end  &&  (ISALNUM(CH(off)) || is_any_of(ctx->text[off], &s2)))
      |                                                                             ^~~
      |                                                                             |
      |                                                                             const char **
/home/foo/.local/c0/include/md4c.h:3444:25: note: expected ‘char *’ but argument is of type ‘const char **’
 3444 | is_any_of(char c, char *p) {
      |                   ~~~~~~^
/home/foo/.local/c0/include/md4c.h: At top level:
/home/foo/.local/c0/include/md4c.h:9887:23: error: initialization of ‘void (*)(const MD_CHAR *, MD_SIZE,  void *)’ {aka ‘void (*)(const char *, unsigned int,  void *)’} from incompatible pointer type ‘void (*)(NU8 *, unsigned int,  void *)’ {aka ‘void (*)(unsigned char *, unsigned int,  void *)’} [-Wincompatible-pointer-types]
 9887 |   .process_output   = nimu_process_markdown,
      |                       ^~~~~~~~~~~~~~~~~~~~~
/home/foo/.local/c0/include/md4c.h:9887:23: note: (near initialization for ‘x.process_output’)
[...]
    Error:  Build failed for the package: chalk
ee7 commented 1 month ago

Fixed by https://github.com/crashappsec/nimutils/pull/72 and https://github.com/crashappsec/chalk/pull/391.