jmcnamara / libxlsxwriter

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

Crash when data validation list exceeds 255 char limit with UTF-8 characters #394

Closed jmcnamara closed 1 year ago

jmcnamara commented 1 year ago

There is an issue with when creating a data validation where the UTF-8 length exceeds 255 characters. Libxlsxwriter checks that the number of characters is less than the Excel 255 limit but uses a 255 8-bit char buffer instead of a 255 UTF-8 char buffer. This leads to a crash as demonstrated by the following code.

#include "xlsxwriter.h"

int main() {

    lxw_workbook  *workbook  = workbook_new("test_data_validation.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
    char *list[] = {
        "8位 无符号",
        "8位 有符号",
        "16位 无符号(AB)",
        "16位 无符号(BA)",
        "16位 有符号(AB)",
        "16位 有符号(BA)",
        "32位 无符号(AB CD)",
        "32位 无符号(CD AB)",
        "32位 有符号(AB CD)",
        "32位 有符号(CD AB)",
        "32位 浮点数(AB CD)",
        "32位 浮点数(CD AB)",
        "64位 浮点数(AB CD)",
        "64位 浮点数(CD AB)",
        "位",
        NULL};

    lxw_data_validation *data_validation = calloc(1, sizeof(lxw_data_validation));
    data_validation->validate = LXW_VALIDATION_TYPE_LIST;
    data_validation->value_list = list;
    data_validation->input_title = "This is the input title";
    data_validation->input_message = "This is the input message";

    worksheet_data_validation_cell(worksheet, CELL("C2"), data_validation);

    free(data_validation);

    return workbook_close(workbook);
}

See also: https://github.com/informationsea/xlsxwriter-rs/issues/43

jmcnamara commented 1 year ago

Fixed on main;

Screenshot 2023-02-22 at 13 09 14