8
8
9
9
#include < boost/stacktrace.hpp>
10
10
#include < stdexcept>
11
+ #include < fstream>
11
12
#include < iostream>
12
13
#include < sstream>
13
14
#include < cctype>
@@ -263,7 +264,51 @@ void test_stacktrace_limits()
263
264
BOOST_TEST_EQ (boost::stacktrace::stacktrace (1 , 1 ).size (), 1 );
264
265
}
265
266
266
- int main () {
267
+ std::size_t get_file_size (const char * file_name) {
268
+ std::ifstream file (file_name, std::ios::binary | std::ios::ate);
269
+ const auto file_size = file.tellg ();
270
+ BOOST_TEST (file_size > 0 );
271
+ return static_cast <std::size_t >(file_size);
272
+ }
273
+
274
+ uintptr_t get_address_from_frame (const std::string& frame) {
275
+ std::size_t address = 0 ;
276
+ std::string hex_address;
277
+ std::size_t pos = frame.find (" 0x" );
278
+
279
+ if (pos != std::string::npos) {
280
+ // Extract the hex address substring
281
+ hex_address = frame.substr (pos + 2 ); // Skip "0x"
282
+
283
+ // Convert hex string to std::size_t
284
+ std::stringstream ss;
285
+ ss << std::hex << hex_address;
286
+ ss >> address;
287
+ }
288
+
289
+ return address;
290
+ }
291
+
292
+ void test_relative_virtual_address (const char * file_path)
293
+ {
294
+ const auto frame = to_string (boost::stacktrace::stacktrace (0 , 1 ).as_vector ().front ());
295
+
296
+ // Skip the test if the frame does not contain an address
297
+ if (frame.find (" 0x" ) == std::string::npos) {
298
+ return ;
299
+ }
300
+
301
+ const auto file_size = get_file_size (file_path);
302
+ BOOST_TEST (file_size > 0 );
303
+
304
+ const auto address = get_address_from_frame (frame);
305
+ BOOST_TEST (address > 0 );
306
+
307
+ // Verify that the address is within the binary
308
+ BOOST_TEST (address <= file_size);
309
+ }
310
+
311
+ int main (const int , const char * argv[]) {
267
312
test_deeply_nested_namespaces ();
268
313
test_frames_string_data_validity ();
269
314
test_nested<15 >();
@@ -283,6 +328,7 @@ int main() {
283
328
test_nested<260 >(false );
284
329
285
330
test_stacktrace_limits ();
331
+ test_relative_virtual_address (argv[0 ]);
286
332
287
333
return boost::report_errors ();
288
334
}
0 commit comments