|
26 | 26 | #include <utils/FixedCapacityVector.h>
|
27 | 27 | #include <utils/Panic.h>
|
28 | 28 | #include <utils/debug.h>
|
29 |
| -#include <utils/ostream.h> |
30 | 29 |
|
31 | 30 | #include <webgpu/webgpu_cpp.h>
|
32 |
| -#include <dawn/webgpu_cpp_print.h> |
33 | 31 |
|
34 | 32 | #include <array>
|
35 | 33 | #include <cstdint>
|
| 34 | +#include <cstdlib> |
| 35 | +#include <limits> |
36 | 36 | #include <sstream>
|
37 |
| -#include <stdexcept> |
38 | 37 | #include <string>
|
39 | 38 | #include <string_view>
|
40 | 39 | #include <unordered_map>
|
@@ -98,15 +97,28 @@ namespace {
|
98 | 97 | }
|
99 | 98 | posOfEqual += posAfterId; // position in original source overall, not just the segment
|
100 | 99 | int constantId = 0;
|
101 |
| - try { |
102 |
| - constantId = std::stoi(idStr.data()); |
103 |
| - } catch (const std::invalid_argument& e) { |
104 |
| - PANIC_POSTCONDITION("Invalid spec constant id '%s' in %s (not a valid integer?): %s", |
105 |
| - idStr.data(), shaderLabel.data(), e.what()); |
106 |
| - } catch (const std::out_of_range& e) { |
107 |
| - PANIC_POSTCONDITION( |
108 |
| - "Invalid spec constant id '%s' in %s (not an integer? out of range?): %s", |
109 |
| - idStr.data(), shaderLabel.data(), e.what()); |
| 100 | + char* endPtr; |
| 101 | + errno = 0; // Clear errno before the call |
| 102 | + |
| 103 | + long tempConstantId = std::strtol(idStr.data(), &endPtr, 10); |
| 104 | + |
| 105 | + // Check for conversion errors |
| 106 | + if (endPtr == idStr.data() || *endPtr != '\0' || errno == ERANGE) { |
| 107 | + // Parsing failed, or conversion was out of range for 'long' |
| 108 | + // or the string contained non-numeric characters after the number. |
| 109 | + PANIC_POSTCONDITION("Invalid spec constant id '%s' in %s (not a valid integer or out " |
| 110 | + "of range for 'long'?)", |
| 111 | + idStr.data(), shaderLabel.data()); |
| 112 | + } else { |
| 113 | + // Check if the parsed long value fits into an int |
| 114 | + if (tempConstantId > std::numeric_limits<int>::max() || |
| 115 | + tempConstantId < std::numeric_limits<int>::min()) { |
| 116 | + PANIC_POSTCONDITION( |
| 117 | + "Invalid spec constant id '%s' in %s (value out of range for 'int')", |
| 118 | + idStr.data(), shaderLabel.data()); |
| 119 | + } else { |
| 120 | + constantId = static_cast<int>(tempConstantId); |
| 121 | + } |
110 | 122 | }
|
111 | 123 | const auto newValueItr = specConstants.find(static_cast<uint32_t>(constantId));
|
112 | 124 | if (newValueItr == specConstants.end()) {
|
|
0 commit comments