|
13 | 13 | // RUN: unix.DynamicMemoryModeling:AddNoOwnershipChangeNotes=true
|
14 | 14 |
|
15 | 15 | #include "Inputs/system-header-simulator-for-malloc.h"
|
| 16 | +// For the tests in namespace protobuf_leak: |
| 17 | +#include "Inputs/system-header-simulator-for-protobuf.h" |
16 | 18 |
|
17 | 19 | //===----------------------------------------------------------------------===//
|
18 | 20 | // Report for which we expect NoOwnershipChangeVisitor to add a new note.
|
@@ -218,3 +220,34 @@ void caller() {
|
218 | 220 | (void)n;
|
219 | 221 | } // no-warning: No potential memory leak here, because that's been already reported.
|
220 | 222 | } // namespace symbol_reaper_lifetime
|
| 223 | + |
| 224 | +// Check that we do not report false positives in automatically generated |
| 225 | +// protobuf code that passes dynamically allocated memory to a certain function |
| 226 | +// named GetOwnedMessageInternal. |
| 227 | +namespace protobuf_leak { |
| 228 | +Arena *some_arena, *some_submessage_arena; |
| 229 | + |
| 230 | +MessageLite *protobuf_leak() { |
| 231 | + MessageLite *p = new MessageLite(); // Real protobuf code instantiates a |
| 232 | + // subclass of MessageLite, but that's |
| 233 | + // not relevant for the bug. |
| 234 | + MessageLite *q = GetOwnedMessageInternal(some_arena, p, some_submessage_arena); |
| 235 | + return q; |
| 236 | + // No leak at end of function -- the pointer escapes in GetOwnedMessageInternal. |
| 237 | +} |
| 238 | + |
| 239 | +void validate_system_header() { |
| 240 | + // The case protobuf_leak would also pass if GetOwnedMessageInternal wasn't |
| 241 | + // declared in a system header. This test verifies that another function |
| 242 | + // declared in the same header behaves differently (doesn't escape memory) to |
| 243 | + // demonstrate that GetOwnedMessageInternal is indeed explicitly recognized |
| 244 | + // by the analyzer. |
| 245 | + |
| 246 | + // expected-note@+1 {{Memory is allocated}} |
| 247 | + MessageLite *p = new MessageLite(); |
| 248 | + SomeOtherFunction(p); |
| 249 | + // expected-warning@+2 {{Potential leak of memory pointed to by 'p'}} |
| 250 | + // expected-note@+1 {{Potential leak of memory pointed to by 'p'}} |
| 251 | +} |
| 252 | + |
| 253 | +} // namespace protobuf_leak |
0 commit comments