Skip to content

Commit

Permalink
Fix 32bit multiply with overflow issue for images.
Browse files Browse the repository at this point in the history
Fix multiply with overflow issue when image locations in the worksheet
were greater than the u32 max value.
  • Loading branch information
jmcnamara committed Aug 12, 2023
1 parent 4aaca54 commit 3be88ba
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 8 deletions.
4 changes: 2 additions & 2 deletions include/xlsxwriter/drawing.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ typedef struct lxw_drawing_object {
uint8_t anchor;
struct lxw_drawing_coords from;
struct lxw_drawing_coords to;
uint32_t col_absolute;
uint32_t row_absolute;
uint64_t col_absolute;
uint64_t row_absolute;
uint32_t width;
uint32_t height;
uint8_t shape;
Expand Down
4 changes: 2 additions & 2 deletions include/xlsxwriter/worksheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1944,8 +1944,8 @@ typedef struct lxw_vml_obj {
lxw_col_t start_col;
int32_t x_offset;
int32_t y_offset;
uint32_t col_absolute;
uint32_t row_absolute;
uint64_t col_absolute;
uint64_t row_absolute;
uint32_t width;
uint32_t height;
double x_dpi;
Expand Down
2 changes: 1 addition & 1 deletion include/xlsxwriter/xmlwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ STAILQ_HEAD(xml_attribute_list, xml_attribute);
/* Create a new attribute struct to add to a xml_attribute_list. */
struct xml_attribute *lxw_new_attribute_str(const char *key,
const char *value);
struct xml_attribute *lxw_new_attribute_int(const char *key, uint32_t value);
struct xml_attribute *lxw_new_attribute_int(const char *key, int64_t value);
struct xml_attribute *lxw_new_attribute_dbl(const char *key, double value);

/* Macro to initialize the xml_attribute_list pointers. */
Expand Down
1 change: 0 additions & 1 deletion src/worksheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2993,7 +2993,6 @@ _worksheet_position_object_pixels(lxw_worksheet *self,
drawing_object->to.row_offset = y2;
drawing_object->col_absolute = x_abs;
drawing_object->row_absolute = y_abs;

}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/xmlwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,12 @@ lxw_new_attribute_str(const char *key, const char *value)

/* Create a new integer XML attribute. */
struct xml_attribute *
lxw_new_attribute_int(const char *key, uint32_t value)
lxw_new_attribute_int(const char *key, int64_t value)
{
struct xml_attribute *attribute = malloc(sizeof(struct xml_attribute));

LXW_ATTRIBUTE_COPY(attribute->key, key);
lxw_snprintf(attribute->value, LXW_MAX_ATTRIBUTE_LENGTH, "%d", value);
lxw_snprintf(attribute->value, LXW_MAX_ATTRIBUTE_LENGTH, "%lld", value);

return attribute;
}
Expand Down
20 changes: 20 additions & 0 deletions test/functional/src/test_image58.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2023, John McNamara, [email protected]
*
*/

#include "xlsxwriter.h"

int main() {

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

worksheet_insert_image(worksheet, CELL("A1048573"), "images/red.png");

return workbook_close(workbook);
}
3 changes: 3 additions & 0 deletions test/functional/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def test_image56(self):
def test_image57(self):
self.run_exe_test('test_image57')

def test_image58(self):
self.run_exe_test('test_image58')

# Test in-memory image handling.
def test_image81(self):
self.run_exe_test('test_image81', 'image01.xlsx')
Expand Down
Binary file added test/functional/xlsx_files/image58.xlsx
Binary file not shown.

0 comments on commit 3be88ba

Please sign in to comment.