Skip to content

Commit 6a19ed2

Browse files
committed
replace memset/memcpy with safe functions if possible
1 parent 7b3be05 commit 6a19ed2

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121

2222
- name: Run clang-tidy
2323
run: |
24-
clang-tidy src/ctx.c --warnings-as-errors=* -- -std=c11 -Isrc
24+
clang-tidy src/ctx.c --warnings-as-errors=* --checks=-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling -- -std=c11 -Isrc
2525
2626
build:
2727
runs-on: ${{ matrix.os }}

src/ctx.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#define _DEFAULT_SOURCE
2+
#include <assert.h>
3+
#include <stdint.h>
24
#include <stdlib.h>
35
#include <string.h>
46
#include <sys/mman.h>
@@ -25,10 +27,20 @@ cj_ctx *create_cj_ctx(void)
2527

2628
void grow_cj_ctx(cj_ctx *ctx)
2729
{
28-
uint64_t half = ctx->size;
29-
ctx->size *= 2;
30-
ctx->mem = realloc(ctx->mem, ctx->size);
31-
memset(ctx->mem + half, 0, half);
30+
if (!ctx)
31+
return;
32+
33+
uint64_t old_size = ctx->size;
34+
uint64_t new_size = old_size * 2;
35+
if (new_size < old_size)
36+
return;
37+
uint8_t *new_mem = realloc(ctx->mem, new_size);
38+
if (!new_mem)
39+
return;
40+
41+
ctx->mem = new_mem;
42+
memset(ctx->mem + old_size, 0, old_size);
43+
ctx->size = new_size;
3244
}
3345

3446
void destroy_cj_ctx(cj_ctx *ctx)
@@ -53,7 +65,9 @@ cj_fn create_cj_fn(cj_ctx *ctx)
5365
return NULL;
5466
}
5567

56-
memcpy(raw + sizeof(uint64_t), ctx->mem, code_size);
68+
assert(ctx->mem);
69+
uint8_t *dest = raw + sizeof(uint64_t);
70+
memcpy(dest, ctx->mem, code_size);
5771
*((uint64_t *)raw) = code_size;
5872

5973
if (mprotect(raw, total_size, PROT_READ | PROT_EXEC) != 0)

0 commit comments

Comments
 (0)