Skip to content

Commit 79db56c

Browse files
committed
Merge remote-tracking branch 'origin/master' into dev/integrate-change-column-name-fix
2 parents 617d9ed + 617e2e5 commit 79db56c

3 files changed

Lines changed: 69 additions & 18 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ if(RAPIDCSV_BUILD_TESTS)
193193
endif()
194194
add_unit_test(test102)
195195
add_unit_test(test103)
196+
add_unit_test(test104)
196197
add_unit_test(test105)
197198

198199
# perf tests

src/rapidcsv.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* rapidcsv.h
33
*
44
* URL: https://github.com/d99kris/rapidcsv
5-
* Version: 8.92
5+
* Version: 8.93
66
*
77
* Copyright (C) 2017-2026 Kristofer Berggren
88
* All rights reserved.
@@ -191,24 +191,16 @@ namespace rapidcsv
191191
pVal = static_cast<T>(std::stoi(pStr));
192192
return;
193193
}
194-
else if (typeid(T) == typeid(signed char))
194+
else if ((typeid(T) == typeid(signed char)) || (typeid(T) == typeid(unsigned char)) ||
195+
(typeid(T) == typeid(short)) || (typeid(T) == typeid(unsigned short)))
195196
{
196-
pVal = static_cast<T>(std::stoi(pStr));
197-
return;
198-
}
199-
else if (typeid(T) == typeid(unsigned char))
200-
{
201-
pVal = static_cast<T>(std::stoi(pStr));
202-
return;
203-
}
204-
else if (typeid(T) == typeid(short))
205-
{
206-
pVal = static_cast<T>(std::stoi(pStr));
207-
return;
208-
}
209-
else if (typeid(T) == typeid(unsigned short))
210-
{
211-
pVal = static_cast<T>(std::stoi(pStr));
197+
const int i = std::stoi(pStr);
198+
if ((i < static_cast<int>((std::numeric_limits<T>::min)())) ||
199+
(i > static_cast<int>((std::numeric_limits<T>::max)())))
200+
{
201+
throw std::out_of_range("conversion: out of range");
202+
}
203+
pVal = static_cast<T>(i);
212204
return;
213205
}
214206
else if (typeid(T) == typeid(long))

tests/test104.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// test104.cpp - narrow integer type range checking
2+
3+
#include <rapidcsv.h>
4+
#include "unittest.h"
5+
6+
int main()
7+
{
8+
int rv = 0;
9+
10+
std::string csv =
11+
"col\n"
12+
"127\n"
13+
"128\n"
14+
"-128\n"
15+
"-129\n"
16+
"255\n"
17+
"256\n"
18+
"32767\n"
19+
"32768\n"
20+
"65535\n"
21+
"65536\n"
22+
;
23+
24+
std::string path = unittest::TempPath();
25+
unittest::WriteFile(path, csv);
26+
27+
try
28+
{
29+
rapidcsv::Document doc(path);
30+
31+
// signed char: valid range [-128, 127]
32+
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<signed char>(0, 0)), 127);
33+
ExpectException(doc.GetCell<signed char>(0, 1), std::out_of_range);
34+
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<signed char>(0, 2)), -128);
35+
ExpectException(doc.GetCell<signed char>(0, 3), std::out_of_range);
36+
37+
// unsigned char: valid range [0, 255]
38+
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<unsigned char>(0, 4)), 255);
39+
ExpectException(doc.GetCell<unsigned char>(0, 5), std::out_of_range);
40+
41+
// short: valid range [-32768, 32767]
42+
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<short>(0, 6)), 32767);
43+
ExpectException(doc.GetCell<short>(0, 7), std::out_of_range);
44+
45+
// unsigned short: valid range [0, 65535]
46+
unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<unsigned short>(0, 8)), 65535);
47+
ExpectException(doc.GetCell<unsigned short>(0, 9), std::out_of_range);
48+
}
49+
catch (const std::exception& ex)
50+
{
51+
std::cout << ex.what() << std::endl;
52+
rv = 1;
53+
}
54+
55+
unittest::DeleteFile(path);
56+
57+
return rv;
58+
}

0 commit comments

Comments
 (0)