informationsea / xlsxwriter-rs

Excel file writer for Rust
https://crates.io/crates/xlsxwriter
Apache License 2.0
265 stars 44 forks source link

call sheet.data_validation_cell crash #43

Open cppcoffee opened 1 year ago

cppcoffee commented 1 year ago

Reproduced rust source code.

main.rs

use anyhow::Result;
use xlsxwriter::{
    DataValidation, DataValidationCriteria, DataValidationErrorType, DataValidationType, Workbook,
};

fn main() -> Result<()> {
    let workbook = Workbook::new("./simple1.xlsx")?;

    let mut sheet1 = workbook.add_worksheet(None)?;
    sheet1.write_string(0, 0, "Red text", None)?;

    let mut validation = DataValidation::new(
        DataValidationType::List,
        DataValidationCriteria::None,
        DataValidationErrorType::Stop,
    );

    let list = build_value_list();

    validation.value_list = Some(list);

    sheet1.data_validation_cell(0, 1, &validation)?;

    workbook.close()?;

    Ok(())
}

fn build_value_list() -> Vec<String> {
    let res = vec![
        "8位 无符号".to_string(),
        "8位 有符号".to_string(),
        "16位 无符号(AB)".to_string(),
        "16位 无符号(BA)".to_string(),
        "16位 有符号(AB)".to_string(),
        "16位 有符号(BA)".to_string(),
        "32位 无符号(AB CD)".to_string(),
        "32位 无符号(CD AB)".to_string(),
        "32位 有符号(AB CD)".to_string(),
        "32位 有符号(CD AB)".to_string(),
        "32位 浮点数(AB CD)".to_string(),
        "32位 浮点数(CD AB)".to_string(),
        "64位 浮点数(AB CD)".to_string(),
        "64位 浮点数(CD AB)".to_string(),
        "位".to_string(),
    ];

    res
}

crash after running cargo run.

crash message:

demo(60168,0x1ed75fa80) malloc: Incorrect checksum for freed object 0x158604f88: probably modified after being freed.
Corrupt value: 0x28b095e6b982e7ae
demo(60168,0x1ed75fa80) malloc: *** set a breakpoint in malloc_error_break to debug
[1]    60168 abort      cargo run

maybe 2007 xlsx limit 255 chars?

jmcnamara commented 1 year ago

This looks like a libxlsxwriter issue. It checks that the number of characters is less than the Excel 255 limit (which it is) but uses a 255 8-bit char buffer instead of a 255 UTF-8 char buffer. Hence the overwrite and crash.

I'll fix it upstream. Tracking at https://github.com/jmcnamara/libxlsxwriter/issues/394

jmcnamara commented 1 year ago

Fixed upstream on libxlsxwriter main.

cppcoffee commented 1 year ago

Wow, that's great!