Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly copy the null terminator #715

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
type: string
steps:
- checkout
- run: apt-get update -y
- run: apt-get install -y --no-install-recommends locales
- run: sed -i '/^# de_DE /s/^# //' /etc/locale.gen
- run: locale-gen
- run: git submodule update --init --recursive
- run: cmake -D XLNT_CXX_LANG=<< parameters.cxx-ver >> -D STATIC=<< parameters.static >> -D BENCHMARKS=<< parameters.benchmarks >> -D TESTS=ON -D SAMPLES=<< parameters.samples >> -D COVERAGE=<< parameters.coverage >> -D CMAKE_BUILD_TYPE=<< parameters.build-type >> .
- run: cmake --build . -- -j2
Expand Down
5 changes: 3 additions & 2 deletions include/xlnt/utils/numeric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ class number_serialiser
return d;
}
char buf[30];
assert(s.size() < sizeof(buf));
auto copy_end = std::copy(s.begin(), s.end(), buf);
assert(s.size() + 1 < sizeof(buf));
const char *cstr = s.c_str();
auto copy_end = std::copy(cstr, cstr + s.size() + 1, buf);
convert_pt_to_comma(buf, static_cast<size_t>(copy_end - buf));
double d = strtod(buf, &end_of_convert);
*len_converted = end_of_convert - buf;
Expand Down
Binary file added tests/data/Issue714_local_comma.xlsx
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/detail/numeric_util_test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <xlnt/utils/numeric.hpp>
#include <helpers/test_suite.hpp>
#include <cstring>

class numeric_test_suite : public test_suite
{
Expand All @@ -36,6 +37,7 @@ class numeric_test_suite : public test_suite
register_test(test_min);
register_test(test_max);
register_test(test_abs);
register_test(test_locale_comma);
}

void test_serialise_number()
Expand Down Expand Up @@ -219,5 +221,18 @@ class numeric_test_suite : public test_suite

static_assert(xlnt::detail::abs(-1.23) == 1.23, "constexpr");
}

void test_locale_comma ()
{
struct SetLocale
{
SetLocale() {xlnt_assert(setlocale(LC_ALL, "de_DE") != nullptr);} // If failed, please install de_DE locale to correctly run this test.
~SetLocale() {setlocale(LC_ALL, "C");}
} setLocale;

xlnt::detail::number_serialiser serialiser;
xlnt_assert(serialiser.deserialise("1.99999999") == 1.99999999);
xlnt_assert(serialiser.deserialise("1.1") == 1.1);
}
};
static numeric_test_suite x;
16 changes: 16 additions & 0 deletions tests/workbook/serialization_test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class serialization_test_suite : public test_suite
register_test(test_Issue503_external_link_load);
register_test(test_formatting);
register_test(test_active_sheet);
register_test(test_locale_comma);
}

bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file)
Expand Down Expand Up @@ -808,6 +809,21 @@ class serialization_test_suite : public test_suite
wb.load(path_helper::test_file("20_active_sheet.xlsx"));
xlnt_assert_equals(wb.active_sheet(), wb[2]);
}

void test_locale_comma ()
{
struct SetLocale
{
SetLocale() {xlnt_assert(setlocale(LC_ALL, "de_DE") != nullptr);} // If failed, please install de_DE locale to correctly run this test.
~SetLocale() {setlocale(LC_ALL, "C");}
} setLocale;

xlnt::workbook wb;
wb.load(path_helper::test_file("Issue714_local_comma.xlsx"));
auto ws = wb.active_sheet();
xlnt_assert_equals(ws.cell("A1").value<double>(), 1.9999999999);
xlnt_assert_equals(ws.cell("A2").value<double>(), 1.1);
}
};

static serialization_test_suite x;