[GH-ISSUE #233] worksheet_insert_image_buffer_opt called > 10000 times causes lib crash #188

Closed
opened 2026-05-05 11:51:45 -06:00 by gitea-mirror · 7 comments
Owner

Originally created by @myseemylife on GitHub (Jun 11, 2019).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/233

Originally assigned to: @jmcnamara on GitHub.

i need write image buffers into xlsx files,then i found worksheet_insert_image_buffer_opt api fit my needs,but when test it with a collection of data(probaly 20000+),it crashed sometimes。so i debug it.
the code is

image_stream = lxw_tmpfile(); worksheet.c line 5635. 

The retured iamge_stream is NULL.,so

fwrite(image_buffer,1,image_size,image_stream) crashed.

But i dont't know why?need help

Originally created by @myseemylife on GitHub (Jun 11, 2019). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/233 Originally assigned to: @jmcnamara on GitHub. i need write image buffers into xlsx files,then i found worksheet_insert_image_buffer_opt api fit my needs,but when test it with a collection of data(probaly 20000+),it crashed sometimes。so i debug it. the code is image_stream = lxw_tmpfile(); worksheet.c line 5635. The retured iamge_stream is NULL.,so fwrite(image_buffer,1,image_size,image_stream) crashed. But i dont't know why?need help
gitea-mirror 2026-05-05 11:51:45 -06:00
Author
Owner

@jmcnamara commented on GitHub (Jun 11, 2019):

Thanks for the report. I'll debug it.

What OS and compiler are you using?

Also, could you try compile with make USE_STANDARD_TMPFILE=1 or with that flag set in the code and see if you get the same error.

<!-- gh-comment-id:500720628 --> @jmcnamara commented on GitHub (Jun 11, 2019): Thanks for the report. I'll debug it. What OS and compiler are you using? Also, could you try compile with `make USE_STANDARD_TMPFILE=1` or with that flag set in the code and see if you get the same error.
Author
Owner

@jmcnamara commented on GitHub (Jun 11, 2019):

For what it is worth I ran the following program that creates 21,000 buffered images without a crash on Fedora.

#include "xlsxwriter.h"


/* Simple array with some PNG data. */
unsigned char image_buffer[] = {
    0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
    0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20,
    0x08, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x18, 0xed, 0xa3, 0x00, 0x00, 0x00,
    0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
    0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc,
    0x61, 0x05, 0x00, 0x00, 0x00, 0x20, 0x63, 0x48, 0x52, 0x4d, 0x00, 0x00,
    0x7a, 0x26, 0x00, 0x00, 0x80, 0x84, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x00,
    0x80, 0xe8, 0x00, 0x00, 0x75, 0x30, 0x00, 0x00, 0xea, 0x60, 0x00, 0x00,
    0x3a, 0x98, 0x00, 0x00, 0x17, 0x70, 0x9c, 0xba, 0x51, 0x3c, 0x00, 0x00,
    0x00, 0x46, 0x49, 0x44, 0x41, 0x54, 0x48, 0x4b, 0x63, 0xfc, 0xcf, 0x40,
    0x63, 0x00, 0xb4, 0x80, 0xa6, 0x88, 0xb6, 0xa6, 0x83, 0x82, 0x87, 0xa6,
    0xce, 0x1f, 0xb5, 0x80, 0x98, 0xe0, 0x1d, 0x8d, 0x03, 0x82, 0xa1, 0x34,
    0x1a, 0x44, 0xa3, 0x41, 0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45,
    0xa3, 0x41, 0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45, 0xa3, 0x41,
    0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45, 0x03, 0x1f, 0x44, 0x00,
    0xaa, 0x35, 0xdd, 0x4e, 0xe6, 0xd5, 0xa1, 0x22, 0x00, 0x00, 0x00, 0x00,
    0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};

unsigned int image_size = 200;


int main() {

    lxw_workbook  *workbook  = workbook_new("image_buffer.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    uint16_t i;
    uint16_t row = 1;


    for (i = 0; i <= 21000; i++)
	    worksheet_insert_image_buffer(worksheet, row++, 0,
					  image_buffer, image_size);

    workbook_close(workbook);

    return 0;
}
<!-- gh-comment-id:500744826 --> @jmcnamara commented on GitHub (Jun 11, 2019): For what it is worth I ran the following program that creates 21,000 buffered images without a crash on Fedora. ```C #include "xlsxwriter.h" /* Simple array with some PNG data. */ unsigned char image_buffer[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x18, 0xed, 0xa3, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 0x00, 0x00, 0x20, 0x63, 0x48, 0x52, 0x4d, 0x00, 0x00, 0x7a, 0x26, 0x00, 0x00, 0x80, 0x84, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x00, 0x80, 0xe8, 0x00, 0x00, 0x75, 0x30, 0x00, 0x00, 0xea, 0x60, 0x00, 0x00, 0x3a, 0x98, 0x00, 0x00, 0x17, 0x70, 0x9c, 0xba, 0x51, 0x3c, 0x00, 0x00, 0x00, 0x46, 0x49, 0x44, 0x41, 0x54, 0x48, 0x4b, 0x63, 0xfc, 0xcf, 0x40, 0x63, 0x00, 0xb4, 0x80, 0xa6, 0x88, 0xb6, 0xa6, 0x83, 0x82, 0x87, 0xa6, 0xce, 0x1f, 0xb5, 0x80, 0x98, 0xe0, 0x1d, 0x8d, 0x03, 0x82, 0xa1, 0x34, 0x1a, 0x44, 0xa3, 0x41, 0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45, 0xa3, 0x41, 0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45, 0xa3, 0x41, 0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45, 0x03, 0x1f, 0x44, 0x00, 0xaa, 0x35, 0xdd, 0x4e, 0xe6, 0xd5, 0xa1, 0x22, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 }; unsigned int image_size = 200; int main() { lxw_workbook *workbook = workbook_new("image_buffer.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); uint16_t i; uint16_t row = 1; for (i = 0; i <= 21000; i++) worksheet_insert_image_buffer(worksheet, row++, 0, image_buffer, image_size); workbook_close(workbook); return 0; } ```
Author
Owner

@jmcnamara commented on GitHub (Jun 12, 2019):

I've pushed a fix that catches the tmpfile error and will prevent the crash. Note, I haven't been able to reproduce this on Linux or macOS with much larger amounts of images.

<!-- gh-comment-id:501071334 --> @jmcnamara commented on GitHub (Jun 12, 2019): I've pushed a fix that catches the tmpfile error and will prevent the crash. Note, I haven't been able to reproduce this on Linux or macOS with much larger amounts of images.
Author
Owner

@myseemylife commented on GitHub (Jun 12, 2019):

thx for your reply~~well,i use libxlsxwriter on windows 10 64bit, vc2015.
i compile it with latest zip lib. i also found another problem. the xlsx file not created after workbook_close() called. it happend somtimes.only when you try writing a huge data into xlsx file.

<!-- gh-comment-id:501081284 --> @myseemylife commented on GitHub (Jun 12, 2019): thx for your reply~~well,i use libxlsxwriter on windows 10 64bit, vc2015. i compile it with latest zip lib. i also found another problem. the xlsx file not created after workbook_close() called. it happend somtimes.only when you try writing a huge data into xlsx file.
Author
Owner

@jmcnamara commented on GitHub (Jun 12, 2019):

i use libxlsxwriter on windows 10 64bit, vc2015.

I tested with the above program, xlsxwriter 0.8.6 and MSVC 2015. The program ran and produced output without any issue. Can you test the sample program above.

<!-- gh-comment-id:501190165 --> @jmcnamara commented on GitHub (Jun 12, 2019): > i use libxlsxwriter on windows 10 64bit, vc2015. I tested with the above program, xlsxwriter 0.8.6 and MSVC 2015. The program ran and produced output without any issue. Can you test the sample program above.
Author
Owner

@jmcnamara commented on GitHub (Jun 12, 2019):

i also found another problem. the xlsx file not created after workbook_close() called. it happend somtimes.only when you try writing a huge data into xlsx file.

You need to open anotherGitHub issue for another issue. When you do please add:

<!-- gh-comment-id:501191290 --> @jmcnamara commented on GitHub (Jun 12, 2019): > i also found another problem. the xlsx file not created after workbook_close() called. it happend somtimes.only when you try writing a huge data into xlsx file. You need to open anotherGitHub issue for another issue. When you do please add: * A sample program that demonstrates the issue * The version of libxlsxwriter that you are using * The size of "huge" * The return value from close(). Preferably via lxw_strerror(). For example like this: https://github.com/jmcnamara/libxlsxwriter/blob/master/examples/anatomy.c#L58
Author
Owner

@myseemylife commented on GitHub (Jun 13, 2019):

this issue has been fixed alreadyso i close it

<!-- gh-comment-id:501641072 --> @myseemylife commented on GitHub (Jun 13, 2019): this issue has been fixed already~so i close it~
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#188
No description provided.