|
1 | 1 | #include "tuner/tuner_util.h" |
2 | 2 |
|
3 | | -// Safely copy std::string to char buffer, ensuring NUL termination and truncation |
| 3 | +// Safely copy std::string to char buffer, ensuring NUL termination and |
| 4 | +// truncation |
4 | 5 | static void safeStrCopy(char *dst, size_t dstSize, const std::string &src) { |
5 | | - if (dstSize == 0) return; |
6 | | - size_t copyLen = std::min(dstSize - 1, src.size()); |
7 | | - if (copyLen > 0) memcpy(dst, src.data(), copyLen); |
8 | | - dst[copyLen] = '\0'; |
| 6 | + if (dstSize == 0) |
| 7 | + return; |
| 8 | + size_t copyLen = std::min(dstSize - 1, src.size()); |
| 9 | + if (copyLen > 0) |
| 10 | + memcpy(dst, src.data(), copyLen); |
| 11 | + dst[copyLen] = '\0'; |
9 | 12 | } |
10 | 13 |
|
11 | 14 | // Generate all combinations and return a vector of flagcxEnvConfig |
12 | 15 | flagcxResult_t generateCandidate(std::vector<struct flagcxEnvConfig> &cfgList) { |
13 | 16 |
|
14 | | - // Return empty if there are no environment variables |
15 | | - if (vars.empty()){ |
16 | | - INFO(FLAGCX_INIT, "Invalid number of environment variables: 0"); |
17 | | - return flagcxInvalidArgument; |
18 | | - } |
| 17 | + // Return empty if there are no environment variables |
| 18 | + if (vars.empty()) { |
| 19 | + INFO(FLAGCX_INIT, "Invalid number of environment variables: 0"); |
| 20 | + return flagcxInvalidArgument; |
| 21 | + } |
19 | 22 |
|
20 | | - // If the number of variables exceeds the structure capacity, truncate |
21 | | - if (vars.size() > (size_t)FLAGCX_ENV_LIST_MAX_LENGTH) { |
22 | | - INFO(FLAGCX_INIT, "The number of environment variables exceeds the maximum length defined by FLAGCX_ENV_LIST_MAX_LENGTH"); |
23 | | - vars.resize(FLAGCX_ENV_LIST_MAX_LENGTH); // Truncate the vars vector |
24 | | - INFO(FLAGCX_INIT, "The number of environment variables has been truncated to FLAGCX_ENV_LIST_MAX_LENGTH (%d)",FLAGCX_ENV_LIST_MAX_LENGTH); |
25 | | - return flagcxSuccess; |
26 | | - } |
| 23 | + // If the number of variables exceeds the structure capacity, truncate |
| 24 | + if (vars.size() > (size_t)FLAGCX_ENV_LIST_MAX_LENGTH) { |
| 25 | + INFO(FLAGCX_INIT, "The number of environment variables exceeds the maximum " |
| 26 | + "length defined by FLAGCX_ENV_LIST_MAX_LENGTH"); |
| 27 | + vars.resize(FLAGCX_ENV_LIST_MAX_LENGTH); // Truncate the vars vector |
| 28 | + INFO(FLAGCX_INIT, |
| 29 | + "The number of environment variables has been truncated to " |
| 30 | + "FLAGCX_ENV_LIST_MAX_LENGTH (%d)", |
| 31 | + FLAGCX_ENV_LIST_MAX_LENGTH); |
| 32 | + return flagcxSuccess; |
| 33 | + } |
27 | 34 |
|
28 | | - // Prepare candidate value lists for each variable (at least one empty string to ensure uniform combination logic) |
29 | | - std::vector<std::vector<std::string>> lists; |
30 | | - lists.reserve(vars.size()); |
31 | | - for (const auto &v : vars) { |
32 | | - if (v.choices.empty()) { |
33 | | - lists.emplace_back(std::vector<std::string>{""}); |
34 | | - } else { |
35 | | - lists.emplace_back(v.choices); |
36 | | - } |
| 35 | + // Prepare candidate value lists for each variable (at least one empty string |
| 36 | + // to ensure uniform combination logic) |
| 37 | + std::vector<std::vector<std::string>> lists; |
| 38 | + lists.reserve(vars.size()); |
| 39 | + for (const auto &v : vars) { |
| 40 | + if (v.choices.empty()) { |
| 41 | + lists.emplace_back(std::vector<std::string>{""}); |
| 42 | + } else { |
| 43 | + lists.emplace_back(v.choices); |
37 | 44 | } |
| 45 | + } |
38 | 46 |
|
39 | | - // Use an index vector to iterate through the Cartesian product (multi-dimensional counter) |
40 | | - size_t nvars = lists.size(); |
41 | | - std::vector<size_t> idx(nvars, 0); |
42 | | - bool done = (nvars == 0); |
43 | | - unsigned long numCandidate = 0; |
| 47 | + // Use an index vector to iterate through the Cartesian product |
| 48 | + // (multi-dimensional counter) |
| 49 | + size_t nvars = lists.size(); |
| 50 | + std::vector<size_t> idx(nvars, 0); |
| 51 | + bool done = (nvars == 0); |
| 52 | + unsigned long numCandidate = 0; |
44 | 53 |
|
45 | | - while (!done) { |
46 | | - // Construct a flagcxEnvConfig and zero-initialize |
47 | | - flagcxEnvConfig cfg; |
48 | | - memset(&cfg, 0, sizeof(cfg)); // this zeroes commTag and all fields; adjust if you want non-zero defaults |
49 | | - |
50 | | - std::string tagStr = "Config " + std::to_string(numCandidate); |
51 | | - if (tagStr.size() < sizeof(cfg.commTag.tag)) { |
52 | | - safeStrCopy(cfg.commTag.tag, sizeof(cfg.commTag.tag), tagStr); |
53 | | - } else { |
54 | | - INFO(FLAGCX_INIT, "Tag string too long, potential buffer overflow"); |
55 | | - return flagcxInvalidArgument; |
56 | | - } |
57 | | - cfg.envCount = 0; |
| 54 | + while (!done) { |
| 55 | + // Construct a flagcxEnvConfig and zero-initialize |
| 56 | + flagcxEnvConfig cfg; |
| 57 | + memset(&cfg, 0, sizeof(cfg)); // this zeroes commTag and all fields; adjust |
| 58 | + // if you want non-zero defaults |
58 | 59 |
|
59 | | - // Fill envs |
60 | | - for (size_t i = 0; i < nvars; ++i) { |
61 | | - flagcxEnvEntity &ent = cfg.envs[i]; |
62 | | - // type |
63 | | - ent.type = FLAGCX_ENV_TYPE_CREATION; |
64 | | - // name |
65 | | - safeStrCopy(ent.name, sizeof(ent.name), vars[i].name); |
66 | | - // value |
67 | | - const std::string &val = lists[i][idx[i]]; |
68 | | - safeStrCopy(ent.value, sizeof(ent.value), val); |
69 | | - // defaultValue |
70 | | - safeStrCopy(ent.defaultValue, sizeof(ent.defaultValue), vars[i].defaultValue); |
| 60 | + std::string tagStr = "Config " + std::to_string(numCandidate); |
| 61 | + if (tagStr.size() < sizeof(cfg.commTag.tag)) { |
| 62 | + safeStrCopy(cfg.commTag.tag, sizeof(cfg.commTag.tag), tagStr); |
| 63 | + } else { |
| 64 | + INFO(FLAGCX_INIT, "Tag string too long, potential buffer overflow"); |
| 65 | + return flagcxInvalidArgument; |
| 66 | + } |
| 67 | + cfg.envCount = 0; |
| 68 | + |
| 69 | + // Fill envs |
| 70 | + for (size_t i = 0; i < nvars; ++i) { |
| 71 | + flagcxEnvEntity &ent = cfg.envs[i]; |
| 72 | + // type |
| 73 | + ent.type = FLAGCX_ENV_TYPE_CREATION; |
| 74 | + // name |
| 75 | + safeStrCopy(ent.name, sizeof(ent.name), vars[i].name); |
| 76 | + // value |
| 77 | + const std::string &val = lists[i][idx[i]]; |
| 78 | + safeStrCopy(ent.value, sizeof(ent.value), val); |
| 79 | + // defaultValue |
| 80 | + safeStrCopy(ent.defaultValue, sizeof(ent.defaultValue), |
| 81 | + vars[i].defaultValue); |
71 | 82 |
|
72 | | - cfg.envCount++; |
73 | | - // Stop if exceeding the maximum allowed envs (should not happen since we truncated vars earlier) |
74 | | - if (cfg.envCount >= FLAGCX_ENV_LIST_MAX_LENGTH) break; |
75 | | - } |
| 83 | + cfg.envCount++; |
| 84 | + // Stop if exceeding the maximum allowed envs (should not happen since we |
| 85 | + // truncated vars earlier) |
| 86 | + if (cfg.envCount >= FLAGCX_ENV_LIST_MAX_LENGTH) |
| 87 | + break; |
| 88 | + } |
76 | 89 |
|
77 | | - cfgList.push_back(cfg); |
| 90 | + cfgList.push_back(cfg); |
78 | 91 |
|
79 | | - // Increment counter (from least significant to most significant) |
80 | | - for (int i = (int)nvars - 1; i >= 0; --i) { |
81 | | - idx[i]++; |
82 | | - if (idx[i] < lists[i].size()) break; |
83 | | - idx[i] = 0; |
84 | | - if (i == 0) done = true; |
85 | | - } |
86 | | - numCandidate += 1; |
| 92 | + // Increment counter (from least significant to most significant) |
| 93 | + for (int i = (int)nvars - 1; i >= 0; --i) { |
| 94 | + idx[i]++; |
| 95 | + if (idx[i] < lists[i].size()) |
| 96 | + break; |
| 97 | + idx[i] = 0; |
| 98 | + if (i == 0) |
| 99 | + done = true; |
87 | 100 | } |
| 101 | + numCandidate += 1; |
| 102 | + } |
88 | 103 |
|
89 | | - return flagcxSuccess; |
| 104 | + return flagcxSuccess; |
90 | 105 | } |
0 commit comments