Skip to content

Commit 4b169f3

Browse files
committed
Add context to error messages for better debugging
- Add function name prefixes to all CobhanBuffer error messages - Add 2GB limit clarification to size-related error messages - Fix set_data_len_bytes message to say "maximum data size" (was "allocation size") - Replace napi_throw_error with NapiUtils::ThrowException in ToString() for consistent error handling via C++ exceptions instead of pending JS exceptions
1 parent 649d0ef commit 4b169f3

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

src/cobhan_buffer.h

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#ifndef COBHAN_BUFFER_H
22
#define COBHAN_BUFFER_H
33

4+
#include "hints.h" // for unlikely
45
#include <cstdint> // for int32_t
56
#include <cstring> // for std::memcpy
67
#include <iostream> // for std::terminate
78
#include <limits> // for std::numeric_limits
89
#include <sstream> // for std::ostringstream
910
#include <stdexcept> // for std::runtime_error, std::invalid_argument
1011
#include <string> // for std::string
11-
#include "hints.h" // for unlikely
1212

1313
#ifdef _WIN32
1414
#include <windows.h> // for SecureZeroMemory
1515
#else
16-
#include <string.h> // for explicit_bzero
16+
#include <string.h> // for explicit_bzero
1717
#endif
1818

1919
class CobhanBuffer {
@@ -22,8 +22,8 @@ class CobhanBuffer {
2222
// data_len_bytes of data
2323
explicit CobhanBuffer(size_t data_len_bytes) {
2424
if (data_len_bytes > max_int32_size) {
25-
throw std::invalid_argument(
26-
"Requested data length exceeds maximum allowable size");
25+
throw std::invalid_argument("CobhanBuffer(size_t): Requested data length "
26+
"exceeds maximum allowable size (2GB limit)");
2727
}
2828
allocation_size = DataSizeToAllocationSize(data_len_bytes);
2929
cbuffer = new char[allocation_size];
@@ -37,7 +37,8 @@ class CobhanBuffer {
3737
: cbuffer(cbuffer), allocation_size(allocation_size), ownership(false) {
3838
if (allocation_size > max_int32_size) {
3939
throw std::invalid_argument(
40-
"Allocation size exceeds maximum allowable size");
40+
"CobhanBuffer(char*, size_t): Allocation size exceeds maximum "
41+
"allowable size (2GB limit)");
4142
}
4243
initialize();
4344
}
@@ -75,7 +76,8 @@ class CobhanBuffer {
7576
// Fallback - volatile to prevent optimization
7677
volatile char *p = data_ptr;
7778
size_t len = get_data_len_bytes();
78-
while (len--) *p++ = 0;
79+
while (len--)
80+
*p++ = 0;
7981
#endif
8082
}
8183
}
@@ -114,23 +116,26 @@ class CobhanBuffer {
114116
+ safety_padding_bytes; // Add safety padding if configured
115117
if (allocation > max_int32_size) {
116118
throw std::invalid_argument(
117-
"Calculated allocation size exceeds maximum allowable size");
119+
"CobhanBuffer::DataSizeToAllocationSize: Calculated allocation size "
120+
"exceeds maximum allowable size (2GB limit)");
118121
}
119122
return allocation;
120123
}
121124

122125
static size_t AllocationSizeToMaxDataSize(size_t allocation_len_bytes) {
123126
// Check for buffer underflow with unlikely hint
124-
constexpr size_t min_size = cobhan_header_size_bytes + canary_size_bytes + safety_padding_bytes;
127+
constexpr size_t min_size =
128+
cobhan_header_size_bytes + canary_size_bytes + safety_padding_bytes;
125129
if (unlikely(allocation_len_bytes < min_size)) {
126130
throw std::invalid_argument("Buffer allocation size too small");
127131
}
128-
132+
129133
size_t data_len_bytes = allocation_len_bytes - cobhan_header_size_bytes -
130134
canary_size_bytes - safety_padding_bytes;
131135
if (data_len_bytes > max_int32_size) {
132136
throw std::invalid_argument(
133-
"Calculated data size exceeds maximum allowable size");
137+
"CobhanBuffer::AllocationSizeToMaxDataSize: Calculated data size "
138+
"exceeds maximum allowable size (2GB limit)");
134139
}
135140
return data_len_bytes;
136141
}
@@ -171,11 +176,13 @@ class CobhanBuffer {
171176
void set_data_len_bytes(size_t data_len_bytes) {
172177
if (data_len_bytes > max_int32_size) {
173178
throw std::invalid_argument(
174-
"Requested data length exceeds maximum allowable size");
179+
"CobhanBuffer::set_data_len_bytes: Requested data length exceeds "
180+
"maximum allowable size (2GB limit)");
175181
}
176182
if (data_len_bytes > max_data_size) {
177183
throw std::invalid_argument(
178-
"Requested data length exceeds allocation size");
184+
"CobhanBuffer::set_data_len_bytes: Requested data length exceeds "
185+
"buffer maximum data size");
179186
}
180187
*data_len_ptr = static_cast<int32_t>(data_len_bytes);
181188
}
@@ -204,7 +211,8 @@ class CobhanBuffer {
204211
}
205212

206213
if (data_len_bytes > max_int32_size) {
207-
throw std::invalid_argument("Data length exceeds maximum allowable size");
214+
throw std::invalid_argument("CobhanBuffer::initialize: Data length "
215+
"exceeds maximum allowable size (2GB limit)");
208216
}
209217

210218
// Write Cobhan header values
@@ -249,7 +257,8 @@ class CobhanBuffer {
249257
allocation_size = other.allocation_size;
250258
if (allocation_size > max_int32_size) {
251259
throw std::invalid_argument(
252-
"Allocation size exceeds maximum allowable size");
260+
"CobhanBuffer::moveFrom: Allocation size exceeds maximum allowable "
261+
"size (2GB limit)");
253262
}
254263

255264
cbuffer = new char[allocation_size];

src/cobhan_buffer_napi.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ class CobhanBufferNapi : public CobhanBuffer {
103103
env, get_data_ptr(), get_data_len_bytes(), &napiStr);
104104

105105
if (status != napi_ok) {
106-
napi_throw_error(env, nullptr,
107-
"Failed to create Napi::String from CobhanBuffer");
108-
return {};
106+
NapiUtils::ThrowException(env, "CobhanBufferNapi::ToString: Failed to "
107+
"create Napi::String from CobhanBuffer");
109108
}
110109

111110
return {env, napiStr};
@@ -198,11 +197,11 @@ class CobhanBufferNapi : public CobhanBuffer {
198197
class SensitiveCobhanBufferNapi : public CobhanBufferNapi {
199198
public:
200199
using CobhanBufferNapi::CobhanBufferNapi; // Inherit all constructors
201-
200+
202201
// Move constructor - needed for async workers
203202
SensitiveCobhanBufferNapi(SensitiveCobhanBufferNapi &&other) noexcept
204203
: CobhanBufferNapi(std::move(other)) {}
205-
204+
206205
// Also allow moving from base class (for async worker initialization)
207206
SensitiveCobhanBufferNapi(CobhanBufferNapi &&other) noexcept
208207
: CobhanBufferNapi(std::move(other)) {}

0 commit comments

Comments
 (0)