Skip to content

Commit adda698

Browse files
committed
Correctly copy the null terminator
Fixes #714
1 parent 297b331 commit adda698

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

include/xlnt/utils/numeric.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ class number_serialiser
176176
}
177177
char buf[30];
178178
assert(s.size() < sizeof(buf));
179-
auto copy_end = std::copy(s.begin(), s.end(), buf);
179+
const char *cstr = s.c_str();
180+
auto copy_end = std::copy(cstr, cstr + s.size() + 1, buf);
180181
convert_pt_to_comma(buf, static_cast<size_t>(copy_end - buf));
181182
double d = strtod(buf, &end_of_convert);
182183
*len_converted = end_of_convert - buf;

tests/detail/numeric_util_test_suite.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <xlnt/utils/numeric.hpp>
2525
#include <helpers/test_suite.hpp>
26+
#include <cstring>
2627

2728
class numeric_test_suite : public test_suite
2829
{
@@ -36,6 +37,7 @@ class numeric_test_suite : public test_suite
3637
register_test(test_min);
3738
register_test(test_max);
3839
register_test(test_abs);
40+
register_test(test_locale_comma);
3941
}
4042

4143
void test_serialise_number()
@@ -219,5 +221,18 @@ class numeric_test_suite : public test_suite
219221

220222
static_assert(xlnt::detail::abs(-1.23) == 1.23, "constexpr");
221223
}
224+
225+
void test_locale_comma ()
226+
{
227+
struct SetLocale
228+
{
229+
SetLocale() {xlnt_assert(setlocale(LC_ALL, "de_DE") != nullptr);} // If failed, please install de_DE locale to correctly run this test.
230+
~SetLocale() {setlocale(LC_ALL, "C");}
231+
} setLocale;
232+
233+
xlnt::detail::number_serialiser serialiser;
234+
xlnt_assert(serialiser.deserialise("1.99999999") == 1.99999999);
235+
xlnt_assert(serialiser.deserialise("1.1") == 1.1);
236+
}
222237
};
223238
static numeric_test_suite x;

tests/workbook/serialization_test_suite.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class serialization_test_suite : public test_suite
7373
register_test(test_Issue503_external_link_load);
7474
register_test(test_formatting);
7575
register_test(test_active_sheet);
76+
register_test(test_locale_comma);
7677
}
7778

7879
bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file)
@@ -808,6 +809,21 @@ class serialization_test_suite : public test_suite
808809
wb.load(path_helper::test_file("20_active_sheet.xlsx"));
809810
xlnt_assert_equals(wb.active_sheet(), wb[2]);
810811
}
812+
813+
void test_locale_comma ()
814+
{
815+
struct SetLocale
816+
{
817+
SetLocale() {xlnt_assert(setlocale(LC_ALL, "de_DE") != nullptr);} // If failed, please install de_DE locale to correctly run this test.
818+
~SetLocale() {setlocale(LC_ALL, "C");}
819+
} setLocale;
820+
821+
xlnt::workbook wb;
822+
wb.load(path_helper::test_file("Issue714_local_comma.xlsx"));
823+
auto ws = wb.active_sheet();
824+
xlnt_assert_equals(ws.cell("A1").value<double>(), 1.9999999999);
825+
xlnt_assert_equals(ws.cell("A2").value<double>(), 1.1);
826+
}
811827
};
812828

813829
static serialization_test_suite x;

0 commit comments

Comments
 (0)