Bug Summary

File:memory_leak_examples.c
Warning:line 11, column 3
Attempt to free released memory

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple thumbv7em-none-unknown-eabi -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name memory_leak_examples.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -nostdsysteminc -target-cpu cortex-m4 -target-feature -crc -target-feature -sha2 -target-feature -aes -target-feature -dotprod -target-feature +dsp -target-feature -mve -target-feature -mve.fp -target-feature -ras -target-feature -sb -target-feature -lob -target-feature -hwdiv-arm -target-feature +hwdiv -target-feature -vfp2 -target-feature +vfp2sp -target-feature -vfp3 -target-feature -vfp3d16 -target-feature +vfp3d16sp -target-feature -vfp3sp -target-feature +fp16 -target-feature -vfp4 -target-feature -vfp4d16 -target-feature +vfp4d16sp -target-feature -vfp4sp -target-feature -fp-armv8 -target-feature -fp-armv8d16 -target-feature -fp-armv8d16sp -target-feature -fp-armv8sp -target-feature -fullfp16 -target-feature -fp64 -target-feature -d32 -target-feature -neon -target-feature -crypto -target-feature -fp16fml -target-feature +strict-align -target-abi aapcs -mfloat-abi hard -fallow-half-arguments-and-returns -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -target-linker-version 556.5 -ffunction-sections -fdata-sections -resource-dir /clang+llvm-10.0.0-x86_64-apple-darwin/lib/clang/10.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I /interrupt/example/freertos-example-llvm/freertos_kernel/include -I /interrupt/example/freertos-example-llvm/include -I /interrupt/example/freertos-example-llvm/freertos_kernel/portable/GCC/ARM_CM4F -internal-isystem /clang+llvm-10.0.0-x86_64-apple-darwin/lib/clang/10.0.0/include -internal-isystem /arm_9_2019q4/bin/../arm-none-eabi/include -Oz -fdebug-compilation-dir /interrupt/example/freertos-example-llvm -ferror-limit 19 -fmessage-length 0 -fno-signed-char -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-slp -analyzer-output=html -faddrsig -o /interrupt/example/freertos-example-llvm/scanbuild_analysis/2020-05-19-190156-30115-1 -x c /interrupt/example/freertos-example-llvm/src/memory_leak_examples.c
1#include "example_project/examples.h"
2
3#include <string.h>
4#include <stdlib.h>
5
6#include "example_project/memory_pool.h"
7
8uint32_t example_malloc_free(void) {
9 const size_t size = 10;
10 uint8_t *ptr = malloc(size);
2
Memory is allocated
11 free(ptr);
3
Attempt to free released memory
12
13 // access the data before setting it
14 uint32_t sum = 0;
15 for (size_t i = 0; i < size; i++) {
16 sum += ptr[i];
17 }
18 return sum;
19}
20
21uint32_t example_access_garbage(void) {
22 const size_t size = 10;
23 uint8_t *ptr = memory_pool_allocate(size);
24 if (ptr == NULL((void*)0)) {
25 return 0;
26 }
27
28 // access the data before setting it
29 uint32_t sum = 0;
30 for (size_t i = 0; i < size; i++) {
31 sum += ptr[i];
32 }
33
34 return sum;
35}
36
37uint32_t example_memory_leak(void) {
38 const size_t size = 10;
39 uint8_t *ptr = memory_pool_allocate(size);
40 if (ptr == NULL((void*)0)) {
41 return 0;
42 }
43
44 // ... do something with the pointer ...
45 const uint8_t return_code = ((uint32_t)(uintptr_t)ptr) ? 0xa5 : 0xef;
46 if (return_code == 0xa5) {
47 return 1;
48 }
49
50 memory_pool_freefree(ptr);
51 return 0;
52}
53
54
55uint32_t example_use_after_free(void) {
56 const size_t size = 10;
57 uint8_t *ptr = memory_pool_allocate(size);
58 if (ptr == NULL((void*)0)) {
59 return 0;
60 }
61
62 // ... do something with the pointer ...
63 const uint8_t return_code = ((uint32_t)(uintptr_t)ptr) ? 0xa5 : 0xef;
64 if (return_code == 0xa5) {
65 memory_pool_freefree(ptr);
66 }
67
68 memset(ptr, 0x5e, size);
69 return 0;
70}
71
72uint32_t example_project_run_memory_leak_examples(void) {
73 return example_malloc_free() + example_access_garbage() + example_memory_leak() + example_use_after_free();
1
Calling 'example_malloc_free'
74}