Skip to content
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ if(RAPIDCSV_BUILD_TESTS)
endif()
add_unit_test(test102)
add_unit_test(test103)
add_unit_test(test104)

# perf tests
add_perf_test(ptest001)
Expand Down
28 changes: 24 additions & 4 deletions src/rapidcsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,42 @@ namespace rapidcsv
}
else if (typeid(T) == typeid(signed char))
{
pVal = static_cast<T>(std::stoi(pStr));
const int i = std::stoi(pStr);
if ((i < std::numeric_limits<signed char>::min()) || (i > std::numeric_limits<signed char>::max()))
{
throw std::out_of_range("stoi: out of signed char range");
}
pVal = static_cast<T>(i);
return;
}
else if (typeid(T) == typeid(unsigned char))
{
pVal = static_cast<T>(std::stoi(pStr));
const int i = std::stoi(pStr);
if ((i < 0) || (i > std::numeric_limits<unsigned char>::max()))
{
throw std::out_of_range("stoi: out of unsigned char range");
}
pVal = static_cast<T>(i);
return;
}
else if (typeid(T) == typeid(short))
{
pVal = static_cast<T>(std::stoi(pStr));
const int i = std::stoi(pStr);
if ((i < std::numeric_limits<short>::min()) || (i > std::numeric_limits<short>::max()))
{
throw std::out_of_range("stoi: out of short range");
}
pVal = static_cast<T>(i);
return;
}
else if (typeid(T) == typeid(unsigned short))
{
pVal = static_cast<T>(std::stoi(pStr));
const int i = std::stoi(pStr);
if ((i < 0) || (i > std::numeric_limits<unsigned short>::max()))
{
throw std::out_of_range("stoi: out of unsigned short range");
}
pVal = static_cast<T>(i);
return;
}
else if (typeid(T) == typeid(long))
Expand Down
58 changes: 58 additions & 0 deletions tests/test104.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// test104.cpp - narrow integer type range checking

#include <rapidcsv.h>
#include "unittest.h"

int main()
{
int rv = 0;

std::string csv =
"col\n"
"127\n"
"128\n"
"-128\n"
"-129\n"
"255\n"
"256\n"
"32767\n"
"32768\n"
"65535\n"
"65536\n"
;

std::string path = unittest::TempPath();
unittest::WriteFile(path, csv);

try
{
rapidcsv::Document doc(path);

// signed char: valid range [-128, 127]
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<signed char>(0, 0)), 127);
ExpectException(doc.GetCell<signed char>(0, 1), std::out_of_range);
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<signed char>(0, 2)), -128);
ExpectException(doc.GetCell<signed char>(0, 3), std::out_of_range);

// unsigned char: valid range [0, 255]
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<unsigned char>(0, 4)), 255);
ExpectException(doc.GetCell<unsigned char>(0, 5), std::out_of_range);

// short: valid range [-32768, 32767]
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<short>(0, 6)), 32767);
ExpectException(doc.GetCell<short>(0, 7), std::out_of_range);

// unsigned short: valid range [0, 65535]
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<unsigned short>(0, 8)), 65535);
ExpectException(doc.GetCell<unsigned short>(0, 9), std::out_of_range);
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
rv = 1;
}

unittest::DeleteFile(path);

return rv;
}
Loading