Categories
Tags
algorithms APIT Arc arm assembly asynchronous base64 BitHacks Blogging box c clang-format client cmake compiler concat concurrency const_fn contravariant cos covariant cpp Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP dtruss Dynamic Example FFI flat_map format FP fsanitize Functional functions futures Fuwari GATs gccrs generics gitignore glibc GUI hacking hashmap haskell heap interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS Markdown math ML mmap nc OnceLock optimization OS panic parallels perf physics pin postgresql radare2 release reverse RPIT rust sanitizer science Science serialization server shift sin SmallProjects socket std strace String StringView strip strlen surrealdb SWAR swisstable synchronous tan toml traits triangulation UnsafeRust utf16 utf8 Video wsl x86_64 xilem zig
226 words
1 minutes
C23_leak_macOS_system_libraries_during_initialization
link
macOS leak 조심하자. 무시해도 되는거구만.
Summary: Your code has no memory leaks. You’re correctly calling
free(s)afterstrdup(). The 120 bytes reported are from macOS system libraries during initialization, which are suppressed with the.lsan.suppfile.The real issue in your output is the typo: “Hello, Worl” - this is because line 8 (
s[n - 1] = 0;) removes the last character ‘d’ since there’s no newline in the original string.
C code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *s = strdup("Hello, World");
int n = strlen(s);
s[n - 1] = 0;
printf("%s\n", s);
printf("sizeof(*s) = %zu\n", sizeof(*s));
free(s);
return 0;
}.lsan.supp추가해주면 무시해준다
❯ eza -la
.rw-r--r-- 251 gy-gyoung 23 Mar 22:21 .clang-format
.rw-r--r-- 507 gy-gyoung 23 Mar 22:21 .gitignore
.rw-r--r-- 100 gy-gyoung 23 Mar 22:23 .lsan.supp.lsan.supp
# Suppress macOS system library leaks
leak:libobjc.A.dylib
leak:libxpc.dylib
leak:libSystem.B.dylibResult
$ LSAN_OPTIONS=suppressions=../.lsan.supp
rm -rf target
mkdir -p target
/opt/homebrew/opt/llvm/bin/clang -g -fsanitize=leak -fno-omit-frame-pointer -c ./src/main.c
/opt/homebrew/opt/llvm/bin/clang -g -fsanitize=leak *.o
mv a.out *.o ./target
./target/a.out
Hello, Worl
sizeof(*s) = 1
-----------------------------------------------------
Suppressions used:
count bytes template
3 120 libobjc.A.dylib
-----------------------------------------------------C23_leak_macOS_system_libraries_during_initialization
https://younghakim7.github.io/blog/posts/c23_leak_macos_system_libraries_during_initialization/