Skip to content

Commit

Permalink
drawing: fix 32bit overflow for uint64_t values
Browse files Browse the repository at this point in the history
Issue #441
  • Loading branch information
jmcnamara committed May 7, 2024
1 parent 15bb08c commit 8d15e88
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/xlsxwriter/xmlwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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, uint64_t value);
struct xml_attribute *lxw_new_attribute_int(const char *key, int32_t value);
struct xml_attribute *lxw_new_attribute_dbl(const char *key, double value);

/* Macro to initialize the xml_attribute_list pointers. */
Expand Down
7 changes: 5 additions & 2 deletions src/drawing.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,11 @@ _drawing_write_a_off(lxw_drawing *self, lxw_drawing_object *drawing_object)
struct xml_attribute *attribute;

LXW_INIT_ATTRIBUTES();
LXW_PUSH_ATTRIBUTES_INT("x", drawing_object->col_absolute);
LXW_PUSH_ATTRIBUTES_INT("y", drawing_object->row_absolute);

/* Use %d to allow for writing uint64_t on ansi 32bit systems. The largest
Excel value will fit without losing precision. */
LXW_PUSH_ATTRIBUTES_DBL("x", drawing_object->col_absolute);
LXW_PUSH_ATTRIBUTES_DBL("y", drawing_object->row_absolute);

lxw_xml_empty_tag(self->file, "a:off", &attributes);

Expand Down
10 changes: 2 additions & 8 deletions src/xmlwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,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, uint64_t value)
lxw_new_attribute_int(const char *key, int32_t value)
{
struct xml_attribute *attribute = malloc(sizeof(struct xml_attribute));

LXW_ATTRIBUTE_COPY(attribute->key, key);

#if defined(_MSC_VER)
lxw_snprintf(attribute->value, LXW_MAX_ATTRIBUTE_LENGTH, "%lld", value);
#else
lxw_snprintf(attribute->value, LXW_MAX_ATTRIBUTE_LENGTH, "%ld",
(long) value);
#endif
lxw_snprintf(attribute->value, LXW_MAX_ATTRIBUTE_LENGTH, "%d", value);

return attribute;
}
Expand Down

0 comments on commit 8d15e88

Please sign in to comment.