mirror of
https://github.com/jmcnamara/libxlsxwriter.git
synced 2026-05-15 14:15:54 -06:00
[GH-ISSUE #151] Problem with number formats #125
Labels
No labels
awaiting user feedback
bug
cmake
cmake
docs
feature request
in progress
long term
medium term
medium term
pull-request
question
question
ready to close
short term
under investigation
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/libxlsxwriter#125
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @franciscopajares on GitHub (Mar 13, 2018).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/151
Originally assigned to: @jmcnamara on GitHub.
Hello.
I am using libslxswriter version 0.7.6 in Windows 7, compiled with Microsoft Visual Studio 2005, and I have found a problem with numeric formats.
The problem is:
When you add two numeric formats with the function format_set_num_format and these formats are similar, i.e "#,##0.0" and "#,##0.00000", sometimes the cells are not formatted with the correct format.
The bug is located in the function _prepare_num_formats in workbook.c .
This function calls to lxw_hash_key_exists, and is not returning the correct format, because the function lxw_hash_key_exists compares the hash with memcmp with the length of the first format. If both formats have different length, but the first characters are equal, the return value is TRUE and should be FALSE.
The formats "#,##0.0" and "#,##0.00000" are different, but when compare with memcmp("#,##0.0", "#,##0.00000", strlen("#,##0.0")) the function returns that they are equal.
Example to reproduce the bug:
The error is in the function _prepare_num_formats located in workbook.c.
The block of code that causes the problem is:
I think that could be replaced by the next piece of code, that compares two hashes with the same length:
I hope it helps.
Kind regards.
@jmcnamara commented on GitHub (Mar 13, 2018):
Hi,
Thanks for the detailed report and the debugging.
I'll put a fix in for it soon.
John
@franciscopajares commented on GitHub (Mar 13, 2018):
Hi,
Thanks to you for saving me so much time developing this library.
Francisco.
@jmcnamara commented on GitHub (Mar 13, 2018):
Here is a quick fix in the meantime.
@franciscopajares commented on GitHub (Mar 13, 2018):
Hi John.
This fix is valid when comparing strings but, is it safe when comparing structs, pointers or another data types?
Thanks again.
@jmcnamara commented on GitHub (Mar 13, 2018):
No. Not fully. I'll need to rethink it. Maybe a solution like yours is better in this case.
@jmcnamara commented on GitHub (Mar 16, 2018):
I've pushed a test and fix for this to master.
@franciscopajares commented on GitHub (Mar 16, 2018):
Hello.
Seems to work fine.
A little question.
Do the lxw_strcpy function fills the memory at the right of the '\0' delimiter with the same value every time that this function is executed?
Thanks,
@jmcnamara commented on GitHub (Mar 16, 2018):
Good eyes.
The value being copied (
format->num_format) will almost always be NULL padded (from initialization) so effectively the storednum_formatvalue will be NULL padded too.However, it is possible that it could contain some garbage if the user reset the
num_formatin the format to a shorter string. This wouldn't affect the formatting in the Excel file however, it would just create a potentially duplicate format.Your solution of
memset + strcpywould avoid even that edge case so maybe I should just use that instead.Thanks,
John
@jmcnamara commented on GitHub (Mar 16, 2018):
I've change the code around a bit to ensure that the stored buffer is alway NULL padded.
Take a look when you get a chance.
@franciscopajares commented on GitHub (Mar 21, 2018):
Hello.
It works fine.
The memory issues are the most difficult to trace and solve, and I think that it was worth it to ensure that the padding on the right was with the same character.
I think that this issue could be marked as solved.
Thank you again for your help.
@jmcnamara commented on GitHub (Mar 21, 2018):
Thanks for the report and follow-up. Closing.