Skip to content

Commit adda943

Browse files
feat(core): add utils function to get env var in a safe way
1 parent 32601f3 commit adda943

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

core/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CODSPEED_VERSION = "1.1.1"
66
# Define the codspeed library
77
cc_library(
88
name = "codspeed",
9-
srcs = glob(["src/**/*.cpp"]),
9+
srcs = glob(["src/**/*.cpp"] + ["src/**/*.h"]),
1010
hdrs = glob(["include/**/*.h"] + ["include/**/*.hpp"]),
1111
includes = ["include"],
1212
defines = [

core/src/utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CODSPEED_UTILS_H
2+
#define CODSPEED_UTILS_H
3+
4+
#include <string>
5+
6+
namespace codspeed {
7+
8+
// Cross-platform getenv wrapper
9+
std::string safe_getenv(const char* var_name);
10+
11+
} // namespace codspeed
12+
13+
#endif // CODSPEED_UTILS_H

core/src/walltime.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string>
1010

1111
#include "codspeed.h"
12+
#include "utils.h"
1213
#ifdef _WIN32
1314
#include <process.h>
1415
#else
@@ -31,7 +32,7 @@ struct BenchmarkStats {
3132
double total_time;
3233
uint64_t iqr_outlier_rounds;
3334
uint64_t stdev_outlier_rounds;
34-
long iter_per_round;
35+
uint64_t iter_per_round;
3536
uint64_t warmup_iters;
3637
};
3738

@@ -169,8 +170,8 @@ void write_codspeed_benchmarks_to_json(
169170
oss << "}";
170171

171172
// Determine the directory path
172-
const char *profile_folder = std::getenv("CODSPEED_PROFILE_FOLDER");
173-
std::string directory = profile_folder ? profile_folder : ".";
173+
std::string profile_folder = safe_getenv("CODSPEED_PROFILE_FOLDER");
174+
std::string directory = profile_folder.empty() ? "." : profile_folder;
174175

175176
// Create the results directory if it does not exist
176177
std::filesystem::path results_path = directory + "/results";

core/src/workspace.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,32 @@
33
#include <filesystem>
44

55
#include "codspeed.h"
6+
#include "utils.h"
67

78
namespace codspeed {
89

9-
std::string get_path_relative_to_workspace(const std::string &path) {
10+
std::string safe_getenv(const char* var_name) {
11+
#ifdef _WIN32
12+
// On Windows, use _dupenv_s instead of std::getenv for thread safety
13+
char* value;
14+
size_t len;
15+
errno_t err = _dupenv_s(&value, &len, var_name);
16+
if (err == 0 && value) {
17+
std::string result(value);
18+
free(value);
19+
return result;
20+
}
21+
return "";
22+
#else
23+
const char* value = std::getenv(var_name);
24+
return value ? value : "";
25+
#endif
26+
}
27+
28+
std::string get_path_relative_to_workspace(const std::string& path) {
1029
// 1. Check for bazel usage, through the BUILD_WORKSPACE_DIRECTORY env var
1130
// If so, __FILE__ will already be relative to the bazel workspace root
12-
if (std::getenv("BUILD_WORKSPACE_DIRECTORY") != NULL) {
31+
if (!safe_getenv("BUILD_WORKSPACE_DIRECTORY").empty()) {
1332
return path;
1433
}
1534

0 commit comments

Comments
 (0)