[GH-ISSUE #485] Bug: clang compilation failed because of fprintf #379

Closed
opened 2026-05-05 12:13:49 -06:00 by gitea-mirror · 9 comments
Owner

Originally created by @mishasychev on GitHub (Aug 18, 2025).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/485

Originally assigned to: @jmcnamara on GitHub.

I'm using libxlsxwriter 1.2.3 in c++ project. Release compilation with all compiler checks enabled fails with error:

error: incompatible pointer to integer conversion passing 'char[43]' to parameter of type 'int' [-Wint-conversion]:
    GOTO_LABEL_ON_MEM_ERROR(custom, mem_error);
note: expanded from macro 'LXW_MEM_ERROR':
    LXW_ERROR("Memory allocation failed.")
note: expanded from macro 'LXW_ERROR':
    LXW_PRINTF(LXW_STDERR "[ERROR][%s:%d]: " message "\n", __FILE__, __LINE__)
note: expanded from macro 'fprintf':
    __fprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__)
note: passing argument to parameter '__flag' here:
    extern int __fprintf_chk (FILE *__restrict __stream, int __flag,

The problem is that clang has an fprintf macro in glibc, so the compiler doesn't understand what is wanted from it. Solution I found is to enclose the fprintf function in brackets:

// include/xlsxwriter/common.h:238
#define LXW_PRINTF (fprintf)

misc: file is also missing some includes (stdio.h, stdint.h)

Originally created by @mishasychev on GitHub (Aug 18, 2025). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/485 Originally assigned to: @jmcnamara on GitHub. I'm using libxlsxwriter 1.2.3 in c++ project. Release compilation with all compiler checks enabled fails with error: ``` error: incompatible pointer to integer conversion passing 'char[43]' to parameter of type 'int' [-Wint-conversion]: GOTO_LABEL_ON_MEM_ERROR(custom, mem_error); note: expanded from macro 'LXW_MEM_ERROR': LXW_ERROR("Memory allocation failed.") note: expanded from macro 'LXW_ERROR': LXW_PRINTF(LXW_STDERR "[ERROR][%s:%d]: " message "\n", __FILE__, __LINE__) note: expanded from macro 'fprintf': __fprintf_chk (stream, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__) note: passing argument to parameter '__flag' here: extern int __fprintf_chk (FILE *__restrict __stream, int __flag, ``` The problem is that clang has an `fprintf` macro in glibc, so the compiler doesn't understand what is wanted from it. Solution I found is to enclose the `fprintf` function in brackets: ```cpp // include/xlsxwriter/common.h:238 #define LXW_PRINTF (fprintf) ``` misc: file is also missing some includes (`stdio.h`, `stdint.h`)
gitea-mirror 2026-05-05 12:13:49 -06:00
Author
Owner

@jmcnamara commented on GitHub (Aug 18, 2025):

Could you let me know:

  • The C/C++ compiler and version.
  • The OS and version.
  • Whether you are using Make or CMake.
  • A complete,simple, program that generates the warning/error.
<!-- gh-comment-id:3198503725 --> @jmcnamara commented on GitHub (Aug 18, 2025): Could you let me know: - The C/C++ compiler and version. - The OS and version. - Whether you are using Make or CMake. - A complete,simple, program that generates the warning/error.
Author
Owner

@mishasychev commented on GitHub (Aug 19, 2025):

  • The C/C++ compiler and version.
Ubuntu clang version 18.1.8 (++20240731024944+3b5b5c1ec4a3-1~exp1~20240731145000.144)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

with glibc.

  • The OS and version.
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.5 LTS
Release:        22.04
Codename:       jammy
  • Whether you are using Make or CMake.

CMake example configuration:

cmake_minimum_required(VERSION 3.12...3.31)
project(libxlsxwriter_project C)

SET(CMAKE_C_STANDARD 11)

# This macro normally comes with compiler checks,
# but we set it manually for simplicity of the configuration file
add_compile_definitions(_FORTIFY_SOURCE=2)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(FetchContent)

FetchContent_Declare(
    libxlsxwriter
    GIT_REPOSITORY https://github.com/jmcnamara/libxlsxwriter.git
    GIT_TAG v1.2.3
)

FetchContent_MakeAvailable(libxlsxwriter)

add_executable(
    libxlsxwriter_example

    main.c
)

target_link_libraries(
    libxlsxwriter_example
    PRIVATE
        xlsxwriter
)
  • A complete,simple, program that generates the warning/error.

clang crashes when building internal lib files, so any use is acceptable as an example:

/*
 * Example of using libxlsxwriter to write a workbook file to a memory buffer.
 *
 * Copyright 2014-2025, John McNamara, jmcnamara@cpan.org
 *
 */

#include <stdio.h>

#include "xlsxwriter.h"

int main() {
    const char *output_buffer;
    size_t output_buffer_size;

    /* Set the worksheet options. */
    lxw_workbook_options options = {.output_buffer = &output_buffer,
                                    .output_buffer_size = &output_buffer_size,
                                    .constant_memory = LXW_FALSE,
                                    .tmpdir = NULL,
                                    .use_zip64 = LXW_FALSE};

    /* Create a new workbook with options. */
    lxw_workbook  *workbook  = workbook_new_opt(NULL, &options);
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
    worksheet_write_number(worksheet, 1, 0, 123,     NULL);

    lxw_error error = workbook_close(workbook);

    if (error)
        return error;

    /* Do something with the XLSX data in the output buffer. */
    FILE *file = fopen("output_buffer.xlsx", "wb");
    fwrite(output_buffer, output_buffer_size, 1, file);
    fclose(file);
    free((void *)output_buffer);

    return ferror(stdout);
}

<!-- gh-comment-id:3201371632 --> @mishasychev commented on GitHub (Aug 19, 2025): > * The C/C++ compiler and version. ``` Ubuntu clang version 18.1.8 (++20240731024944+3b5b5c1ec4a3-1~exp1~20240731145000.144) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin ``` with `glibc`. > * The OS and version. ``` No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.5 LTS Release: 22.04 Codename: jammy ``` > * Whether you are using Make or CMake. CMake example configuration: ```cmake cmake_minimum_required(VERSION 3.12...3.31) project(libxlsxwriter_project C) SET(CMAKE_C_STANDARD 11) # This macro normally comes with compiler checks, # but we set it manually for simplicity of the configuration file add_compile_definitions(_FORTIFY_SOURCE=2) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") include(FetchContent) FetchContent_Declare( libxlsxwriter GIT_REPOSITORY https://github.com/jmcnamara/libxlsxwriter.git GIT_TAG v1.2.3 ) FetchContent_MakeAvailable(libxlsxwriter) add_executable( libxlsxwriter_example main.c ) target_link_libraries( libxlsxwriter_example PRIVATE xlsxwriter ) ``` > * A complete,simple, program that generates the warning/error. clang crashes when building internal lib files, so any use is acceptable as an example: ```c /* * Example of using libxlsxwriter to write a workbook file to a memory buffer. * * Copyright 2014-2025, John McNamara, jmcnamara@cpan.org * */ #include <stdio.h> #include "xlsxwriter.h" int main() { const char *output_buffer; size_t output_buffer_size; /* Set the worksheet options. */ lxw_workbook_options options = {.output_buffer = &output_buffer, .output_buffer_size = &output_buffer_size, .constant_memory = LXW_FALSE, .tmpdir = NULL, .use_zip64 = LXW_FALSE}; /* Create a new workbook with options. */ lxw_workbook *workbook = workbook_new_opt(NULL, &options); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); worksheet_write_string(worksheet, 0, 0, "Hello", NULL); worksheet_write_number(worksheet, 1, 0, 123, NULL); lxw_error error = workbook_close(workbook); if (error) return error; /* Do something with the XLSX data in the output buffer. */ FILE *file = fopen("output_buffer.xlsx", "wb"); fwrite(output_buffer, output_buffer_size, 1, file); fclose(file); free((void *)output_buffer); return ferror(stdout); } ```
Author
Owner

@jmcnamara commented on GitHub (Aug 20, 2025):

Thanks for the detail.

I cannot reproduce this using Ubuntu 24 (the only version I had to hand) and clang 18 using the provided sample CMake file and example.

The sample

$ clang --version

Ubuntu clang version 18.1.3 (1ubuntu1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

$ lab_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.2 LTS
Release:        24.04
Codename:       noble

Are you sure that the provided example demonstrates the error?

Also, can you reproduce it on any other system?

Libxlsxwriter has been tested extensively on Ubuntu (and other Linuxes) with gcc and clang and this is the first time this issue has been reported. There is also a C++ test case in the test suite.

<!-- gh-comment-id:3204649334 --> @jmcnamara commented on GitHub (Aug 20, 2025): Thanks for the detail. I cannot reproduce this using Ubuntu 24 (the only version I had to hand) and clang 18 using the provided sample CMake file and example. The sample ```bash $ clang --version Ubuntu clang version 18.1.3 (1ubuntu1) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin $ lab_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 24.04.2 LTS Release: 24.04 Codename: noble ``` Are you sure that the provided example demonstrates the error? Also, can you reproduce it on any other system? Libxlsxwriter has been tested extensively on Ubuntu (and other Linuxes) with gcc and clang and this is the first time this issue has been reported. There is also a C++ test case in the test suite.
Author
Owner

@mishasychev commented on GitHub (Aug 20, 2025):

@jmcnamara, I created a repo with an error reproduction. The ci failed with the exact same error we’re encountering

<!-- gh-comment-id:3207204802 --> @mishasychev commented on GitHub (Aug 20, 2025): @jmcnamara, I created a [repo](https://github.com/mishasychev/libxlsxwriter-example) with an error reproduction. The ci failed with the exact same error we’re encountering
Author
Owner

@jmcnamara commented on GitHub (Aug 20, 2025):

Thanks. I'll look into it.

<!-- gh-comment-id:3207343253 --> @jmcnamara commented on GitHub (Aug 20, 2025): Thanks. I'll look into it.
Author
Owner

@jmcnamara commented on GitHub (Aug 20, 2025):

Looking at the sample repo it seems that this issue manifests itself when:

  • The compiler is clang (but not gcc).
  • The compilation mode is Release (bug not Debug).
  • _FORTIFY_SOURCE>1, which is the main cause of the issue since clang replaces fprintf() with a stack overflow checking version.

The test macros manpage says the following:

_FORTIFY_SOURCE set to 2, some more checking is added, but some conforming programs might fail.

So the easiest thing to do would be to turn off FORTIFY_SOURCE or set it to 1 when compiling libxlsxwriter.

<!-- gh-comment-id:3208463599 --> @jmcnamara commented on GitHub (Aug 20, 2025): Looking at the sample repo it seems that this issue manifests itself when: * The compiler is `clang` (but not `gcc`). * The compilation mode is `Release` (bug not `Debug`). * `_FORTIFY_SOURCE>1`, which is the main cause of the issue since `clang` replaces `fprintf()` with a stack overflow checking version. The [test macros manpage](https://www.man7.org/linux/man-pages/man7/feature_test_macros.7.html) says the following: >**_FORTIFY_SOURCE** set to 2, some more checking is added, but some conforming programs might fail. So the easiest thing to do would be to turn off `FORTIFY_SOURCE` or set it to 1 when compiling `libxlsxwriter`.
Author
Owner

@mishasychev commented on GitHub (Aug 21, 2025):

  • The compilation mode is Release (bug not Debug).

Sorry for not mentioning this earlier.

  • So the easiest thing to do would be to turn off FORTIFY_SOURCE or set it to 1 when compiling libxlsxwriter.

I don't think it's a good option to disable some compiler checks unless they are false positive, they can be helpful in other places in the code. In our case there is a simple solution to avoid build problems and keep fortify verification enabled. I can create a pr if you agree.

<!-- gh-comment-id:3209226362 --> @mishasychev commented on GitHub (Aug 21, 2025): > * The compilation mode is `Release` (bug not `Debug`). Sorry for not mentioning this earlier. > * So the easiest thing to do would be to turn off `FORTIFY_SOURCE` or set it to 1 when compiling `libxlsxwriter`. I don't think it's a good option to disable some compiler checks unless they are false positive, they can be helpful in other places in the code. In our case there is a simple solution to avoid build problems and keep fortify verification enabled. I can create a pr if you agree.
Author
Owner

@jmcnamara commented on GitHub (Aug 21, 2025):

Sorry for not mentioning this earlier.

It would have saved us both time.

I don't think it's a good option to disable some compiler checks unless they are false positive, they can be helpful in other places in the code.

I'm only suggesting turning it off for the libxlsxwriter compilation. Is there some technical reason you can't do that?

Also, for me this is a false positive: it only happens with clang and the FORTIFY_SOURCE docs says "conforming programs might fail" which is exactly what happens.

<!-- gh-comment-id:3209306030 --> @jmcnamara commented on GitHub (Aug 21, 2025): > Sorry for not mentioning this earlier. It would have saved us both time. > I don't think it's a good option to disable some compiler checks unless they are false positive, they can be helpful in other places in the code. I'm only suggesting turning it off for the **libxlsxwriter** compilation. Is there some technical reason you can't do that? Also, for me this *is* a false positive: it only happens with clang and the `FORTIFY_SOURCE` docs says "conforming programs might fail" which is exactly what happens.
Author
Owner

@jmcnamara commented on GitHub (Aug 29, 2025):

Closing.

<!-- gh-comment-id:3236470907 --> @jmcnamara commented on GitHub (Aug 29, 2025): Closing.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#379
No description provided.