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
1919class 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];
0 commit comments