jmcnamara / libxlsxwriter

A C library for creating Excel XLSX files.
https://libxlsxwriter.github.io
Other
1.53k stars 336 forks source link

Bug: workbook_add_worksheet does not return a reasonable worksheet #443

Closed wxie7 closed 6 months ago

wxie7 commented 6 months ago

There may be a semantic bug here. When name is NULL, but the same name has been written before, a worksheet with a new valid name should be returned.

#include "xlsxwriter.h"
int main() {
    lxw_workbook  *workbook  = workbook_new("demo.xlsx");
    const char* name[] = {"Sheet1", "Sheet2", "Sheet4", NULL};
    for (int i = 0; i < sizeof(name) / sizeof(*name); ++i) {
      if (name[i] == NULL || workbook_validate_sheet_name(workbook, name[i]) == LXW_NO_ERROR) {
        lxw_worksheet *worksheet = workbook_add_worksheet(workbook, name[i]);
        if (worksheet == NULL) {
          __builtin_trap();
        }
      }
    }
    return 0;
}
jmcnamara commented 6 months ago

For me this isn't a bug. The default sheet naming is intended to be simple and incremental. If the user has a scenario where they want to use one of the default names out of sequence then they should avoid using default naming or at least add some logic to avoid this issue.

Closing as won't fix.