Commit 19d7f4dd82

Ryan Liptak <squeek502@hotmail.com>
2022-06-24 02:27:55
FailingAllocator: Only capture the stack trace of the first induced allocation failure
This is a precaution to avoid confusing stack traces on the off chance that FailingAllocator continues to try to allocate after the first failure.
1 parent c321b2f
Changed files (1)
lib
lib/std/testing/failing_allocator.zig
@@ -56,13 +56,15 @@ pub const FailingAllocator = struct {
         return_address: usize,
     ) error{OutOfMemory}![]u8 {
         if (self.index == self.fail_index) {
-            mem.set(usize, &self.stack_addresses, 0);
-            var stack_trace = std.builtin.StackTrace{
-                .instruction_addresses = &self.stack_addresses,
-                .index = 0,
-            };
-            std.debug.captureStackTrace(return_address, &stack_trace);
-            self.has_induced_failure = true;
+            if (!self.has_induced_failure) {
+                mem.set(usize, &self.stack_addresses, 0);
+                var stack_trace = std.builtin.StackTrace{
+                    .instruction_addresses = &self.stack_addresses,
+                    .index = 0,
+                };
+                std.debug.captureStackTrace(return_address, &stack_trace);
+                self.has_induced_failure = true;
+            }
             return error.OutOfMemory;
         }
         const result = try self.internal_allocator.rawAlloc(len, ptr_align, len_align, return_address);