Skip to content

Commit 0f45e25

Browse files
Copilotbbockelm
andcommitted
Address code review comments
- Use std::array instead of plain C array for better type safety - Replace catch-all exception handler with specific std::invalid_argument and std::out_of_range - Use std::stoi with exception handling instead of std::atoi in tests - Fix include path in test to use public header instead of relative path Co-authored-by: bbockelm <1093447+bbockelm@users.noreply.github.com>
1 parent ef95dcb commit 0f45e25

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

src/scitokens.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <cstdlib>
66
#include <algorithm>
77
#include <cctype>
8+
#include <array>
9+
#include <stdexcept>
810

911
#include "scitokens.h"
1012
#include "scitokens_internal.h"
@@ -40,12 +42,12 @@ void load_config_from_environment() {
4042
bool is_int;
4143
};
4244

43-
const ConfigMapping known_configs[] = {
45+
const std::array<ConfigMapping, 4> known_configs = {{
4446
{"keycache.update_interval_s", "KEYCACHE_UPDATE_INTERVAL_S", true},
4547
{"keycache.expiration_interval_s", "KEYCACHE_EXPIRATION_INTERVAL_S", true},
4648
{"keycache.cache_home", "KEYCACHE_CACHE_HOME", false},
4749
{"tls.ca_file", "TLS_CA_FILE", false}
48-
};
50+
}};
4951

5052
const char *prefix = "SCITOKEN_CONFIG_";
5153

@@ -71,8 +73,10 @@ void load_config_from_environment() {
7173
try {
7274
int value = std::stoi(env_value);
7375
scitoken_config_set_int(config.config_key, value, &err_msg);
74-
} catch (...) {
75-
// Silently ignore parse errors during initialization
76+
} catch (const std::invalid_argument &) {
77+
// Silently ignore invalid integer format during initialization
78+
} catch (const std::out_of_range &) {
79+
// Silently ignore out-of-range values during initialization
7680
}
7781
} else {
7882
scitoken_config_set_str(config.config_key, env_value, &err_msg);

test/test_env_config.cpp

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* before the library is loaded to properly test the constructor function.
55
*/
66

7-
#include "../src/scitokens.h"
7+
#include "scitokens.h"
88
#include <cstdlib>
99
#include <cstring>
1010
#include <iostream>
11+
#include <stdexcept>
1112

1213
int main() {
1314
int failures = 0;
@@ -16,38 +17,48 @@ int main() {
1617
// Test 1: Check if SCITOKEN_CONFIG_KEYCACHE_UPDATE_INTERVAL_S was loaded
1718
const char *env_update = std::getenv("SCITOKEN_CONFIG_KEYCACHE_UPDATE_INTERVAL_S");
1819
if (env_update) {
19-
int expected = std::atoi(env_update);
20-
int actual = scitoken_config_get_int("keycache.update_interval_s", &err_msg);
21-
if (actual != expected) {
22-
std::cerr << "FAIL: keycache.update_interval_s expected " << expected
23-
<< " but got " << actual << std::endl;
24-
if (err_msg) {
25-
std::cerr << "Error: " << err_msg << std::endl;
26-
free(err_msg);
27-
err_msg = nullptr;
20+
try {
21+
int expected = std::stoi(env_update);
22+
int actual = scitoken_config_get_int("keycache.update_interval_s", &err_msg);
23+
if (actual != expected) {
24+
std::cerr << "FAIL: keycache.update_interval_s expected " << expected
25+
<< " but got " << actual << std::endl;
26+
if (err_msg) {
27+
std::cerr << "Error: " << err_msg << std::endl;
28+
free(err_msg);
29+
err_msg = nullptr;
30+
}
31+
failures++;
32+
} else {
33+
std::cout << "PASS: keycache.update_interval_s = " << actual << std::endl;
2834
}
35+
} catch (const std::exception &e) {
36+
std::cerr << "FAIL: Could not parse env var value: " << e.what() << std::endl;
2937
failures++;
30-
} else {
31-
std::cout << "PASS: keycache.update_interval_s = " << actual << std::endl;
3238
}
3339
}
3440

3541
// Test 2: Check if SCITOKEN_CONFIG_KEYCACHE_EXPIRATION_INTERVAL_S was loaded
3642
const char *env_expiry = std::getenv("SCITOKEN_CONFIG_KEYCACHE_EXPIRATION_INTERVAL_S");
3743
if (env_expiry) {
38-
int expected = std::atoi(env_expiry);
39-
int actual = scitoken_config_get_int("keycache.expiration_interval_s", &err_msg);
40-
if (actual != expected) {
41-
std::cerr << "FAIL: keycache.expiration_interval_s expected " << expected
42-
<< " but got " << actual << std::endl;
43-
if (err_msg) {
44-
std::cerr << "Error: " << err_msg << std::endl;
45-
free(err_msg);
46-
err_msg = nullptr;
44+
try {
45+
int expected = std::stoi(env_expiry);
46+
int actual = scitoken_config_get_int("keycache.expiration_interval_s", &err_msg);
47+
if (actual != expected) {
48+
std::cerr << "FAIL: keycache.expiration_interval_s expected " << expected
49+
<< " but got " << actual << std::endl;
50+
if (err_msg) {
51+
std::cerr << "Error: " << err_msg << std::endl;
52+
free(err_msg);
53+
err_msg = nullptr;
54+
}
55+
failures++;
56+
} else {
57+
std::cout << "PASS: keycache.expiration_interval_s = " << actual << std::endl;
4758
}
59+
} catch (const std::exception &e) {
60+
std::cerr << "FAIL: Could not parse env var value: " << e.what() << std::endl;
4861
failures++;
49-
} else {
50-
std::cout << "PASS: keycache.expiration_interval_s = " << actual << std::endl;
5162
}
5263
}
5364

@@ -100,19 +111,24 @@ int main() {
100111
// Test 5: Test case insensitivity (lowercase env var)
101112
const char *env_lower = std::getenv("scitoken_config_keycache_update_interval_s");
102113
if (env_lower) {
103-
int expected = std::atoi(env_lower);
104-
int actual = scitoken_config_get_int("keycache.update_interval_s", &err_msg);
105-
if (actual != expected) {
106-
std::cerr << "FAIL: lowercase env var - keycache.update_interval_s expected "
107-
<< expected << " but got " << actual << std::endl;
108-
if (err_msg) {
109-
std::cerr << "Error: " << err_msg << std::endl;
110-
free(err_msg);
111-
err_msg = nullptr;
114+
try {
115+
int expected = std::stoi(env_lower);
116+
int actual = scitoken_config_get_int("keycache.update_interval_s", &err_msg);
117+
if (actual != expected) {
118+
std::cerr << "FAIL: lowercase env var - keycache.update_interval_s expected "
119+
<< expected << " but got " << actual << std::endl;
120+
if (err_msg) {
121+
std::cerr << "Error: " << err_msg << std::endl;
122+
free(err_msg);
123+
err_msg = nullptr;
124+
}
125+
failures++;
126+
} else {
127+
std::cout << "PASS: lowercase env var - keycache.update_interval_s = " << actual << std::endl;
112128
}
129+
} catch (const std::exception &e) {
130+
std::cerr << "FAIL: Could not parse env var value: " << e.what() << std::endl;
113131
failures++;
114-
} else {
115-
std::cout << "PASS: lowercase env var - keycache.update_interval_s = " << actual << std::endl;
116132
}
117133
}
118134

0 commit comments

Comments
 (0)