Skip to content

Commit ea6b39d

Browse files
jpnurmiclaude
andauthored
ref: zero-initialize SENTRY_MAKE allocations (#1546)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e7e9335 commit ea6b39d

20 files changed

Lines changed: 23 additions & 36 deletions

src/backends/native/sentry_crash_ipc.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ sentry__crash_ipc_init_app(sem_t *init_sem)
2323
if (!ipc) {
2424
return NULL;
2525
}
26-
memset(ipc, 0, sizeof(sentry_crash_ipc_t));
2726
ipc->is_daemon = false;
2827
ipc->init_sem = init_sem; // Use provided semaphore (managed by backend)
2928

@@ -179,7 +178,6 @@ sentry__crash_ipc_init_daemon(
179178
if (!ipc) {
180179
return NULL;
181180
}
182-
memset(ipc, 0, sizeof(sentry_crash_ipc_t));
183181
ipc->is_daemon = true;
184182

185183
// Open existing shared memory created by app (using PID and thread ID)
@@ -316,7 +314,6 @@ sentry__crash_ipc_init_app(sem_t *init_sem)
316314
if (!ipc) {
317315
return NULL;
318316
}
319-
memset(ipc, 0, sizeof(sentry_crash_ipc_t));
320317
ipc->is_daemon = false;
321318
ipc->init_sem = init_sem; // Use provided semaphore (managed by backend)
322319

@@ -474,7 +471,6 @@ sentry__crash_ipc_init_daemon(
474471
if (!ipc) {
475472
return NULL;
476473
}
477-
memset(ipc, 0, sizeof(sentry_crash_ipc_t));
478474
ipc->is_daemon = true;
479475

480476
// Open existing shared memory created by app (using PID and thread ID)
@@ -608,7 +604,6 @@ sentry__crash_ipc_init_app(HANDLE init_mutex)
608604
if (!ipc) {
609605
return NULL;
610606
}
611-
memset(ipc, 0, sizeof(sentry_crash_ipc_t));
612607
ipc->is_daemon = false;
613608
ipc->init_mutex = init_mutex; // Use provided mutex (managed by backend)
614609

@@ -738,7 +733,6 @@ sentry__crash_ipc_init_daemon(pid_t app_pid, uint64_t app_tid,
738733
if (!ipc) {
739734
return NULL;
740735
}
741-
memset(ipc, 0, sizeof(sentry_crash_ipc_t));
742736
ipc->is_daemon = true;
743737

744738
// Open existing shared memory (using PID and thread ID)

src/backends/sentry_backend_breakpad.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ sentry__backend_new(void)
335335
if (!backend) {
336336
return nullptr;
337337
}
338-
memset(backend, 0, sizeof(sentry_backend_t));
339338

340339
backend->startup_func = breakpad_backend_startup;
341340
backend->shutdown_func = breakpad_backend_shutdown;

src/backends/sentry_backend_crashpad.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,6 @@ sentry__backend_new(void)
10411041
if (!backend) {
10421042
return nullptr;
10431043
}
1044-
memset(backend, 0, sizeof(sentry_backend_t));
10451044

10461045
auto *data = new (std::nothrow) crashpad_state_t {};
10471046
if (!data) {

src/backends/sentry_backend_inproc.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,6 @@ sentry__backend_new(void)
18141814
if (!backend) {
18151815
return NULL;
18161816
}
1817-
memset(backend, 0, sizeof(sentry_backend_t));
18181817

18191818
backend->startup_func = startup_inproc_backend;
18201819
backend->shutdown_func = shutdown_inproc_backend;

src/backends/sentry_backend_native.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ native_backend_startup(
121121
if (!state) {
122122
return 1;
123123
}
124-
memset(state, 0, sizeof(native_backend_state_t));
125124
backend->data = state;
126125

127126
// Initialize IPC (protected by global synchronization for concurrent
@@ -935,8 +934,6 @@ sentry__backend_new(void)
935934
return NULL;
936935
}
937936

938-
memset(backend, 0, sizeof(sentry_backend_t));
939-
940937
backend->startup_func = native_backend_startup;
941938
backend->shutdown_func = native_backend_shutdown;
942939
backend->free_func = native_backend_free;

src/sentry_alloc.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ sentry_malloc(size_t size)
2121
return malloc(size);
2222
}
2323

24+
void *
25+
sentry__calloc(size_t count, size_t size)
26+
{
27+
if (count && size > SIZE_MAX / count) {
28+
return NULL;
29+
}
30+
#ifdef WITH_PAGE_ALLOCATOR
31+
if (sentry__page_allocator_enabled()) {
32+
// the page allocator is a bump allocator backed by mmap(MAP_ANONYMOUS),
33+
// which the OS guarantees to be zeroed on first use, and the page
34+
// allocator never reuses freed allocations
35+
return sentry__page_allocator_alloc(count * size);
36+
}
37+
#endif
38+
return calloc(count, size);
39+
}
40+
2441
void
2542
sentry_free(void *ptr)
2643
{

src/sentry_alloc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
#include "sentry_boot.h"
55

6+
void *sentry__calloc(size_t count, size_t size);
7+
68
/**
7-
* This is a shortcut for a typed `malloc`.
9+
* This is a shortcut for a typed `calloc` that zero-initializes the allocation.
810
*/
9-
#define SENTRY_MAKE(Type) (Type *)sentry_malloc(sizeof(Type))
11+
#define SENTRY_MAKE(Type) (Type *)sentry__calloc(1, sizeof(Type))
1012

1113
#endif

src/sentry_attachment.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ sentry__attachment_from_path(sentry_path_t *path)
7979
sentry__path_free(path);
8080
return NULL;
8181
}
82-
memset(attachment, 0, sizeof(sentry_attachment_t));
8382
attachment->path = path;
8483
return attachment;
8584
}
@@ -100,7 +99,6 @@ sentry__attachment_from_buffer(
10099
sentry__path_free(filename);
101100
return NULL;
102101
}
103-
memset(attachment, 0, sizeof(sentry_attachment_t));
104102
attachment->filename = filename;
105103
attachment->buf = sentry_malloc(buf_len * sizeof(char));
106104
memcpy(attachment->buf, buf, buf_len * sizeof(char));
@@ -220,7 +218,6 @@ attachment_clone(const sentry_attachment_t *attachment)
220218
if (!clone) {
221219
return NULL;
222220
}
223-
memset(clone, 0, sizeof(sentry_attachment_t));
224221

225222
if (attachment->path) {
226223
clone->path = sentry__path_clone(attachment->path);

src/sentry_batcher.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "sentry_cpu_relax.h"
44
#include "sentry_options.h"
55
#include "sentry_utils.h"
6-
#include <string.h>
76

87
// The batcher thread sleeps for this interval between flush cycles.
98
// When the timer fires and there are items in the buffer, they are flushed
@@ -28,7 +27,6 @@ sentry__batcher_new(
2827
if (!batcher) {
2928
return NULL;
3029
}
31-
memset(batcher, 0, sizeof(sentry_batcher_t));
3230
batcher->refcount = 1;
3331
batcher->batch_func = batch_func;
3432
batcher->data_category = data_category;

src/sentry_hint.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ sentry_hint_new(void)
1414
if (!hint) {
1515
return NULL;
1616
}
17-
memset(hint, 0, sizeof(sentry_hint_t));
1817
return hint;
1918
}
2019

0 commit comments

Comments
 (0)