Skip to content

Commit 2e8bc17

Browse files
committedFeb 17, 2024
Fix up GCC analyzer
1 parent eefa9bf commit 2e8bc17

File tree

10 files changed

+72
-42
lines changed

10 files changed

+72
-42
lines changed
 

‎Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ FUZZ_OUTPUT_DIR = $(shell pwd)/fuzz/output
1111
SOEXT := $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]')
1212

1313
CPPFLAGS := -Iinclude
14-
CFLAGS := -g -O2 -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -Wno-missing-braces -fPIC -fvisibility=hidden
14+
CFLAGS := -g -O2 -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -Wno-missing-braces -fPIC -fvisibility=hidden $(CFLAGS)
1515
CC := cc
1616
WASI_SDK_PATH := /opt/wasi-sdk
1717

‎ext/prism/extension.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ build_options_scopes(pm_options_t *options, VALUE scopes) {
8282

8383
// Initialize the scopes array.
8484
size_t scopes_count = RARRAY_LEN(scopes);
85-
pm_options_scopes_init(options, scopes_count);
85+
if (!pm_options_scopes_init(options, scopes_count)) {
86+
rb_raise(rb_eNoMemError, "failed to allocate memory");
87+
}
8688

8789
// Iterate over the scopes and add them to the options.
8890
for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
@@ -97,7 +99,9 @@ build_options_scopes(pm_options_t *options, VALUE scopes) {
9799
// Initialize the scope array.
98100
size_t locals_count = RARRAY_LEN(scope);
99101
pm_options_scope_t *options_scope = &options->scopes[scope_index];
100-
pm_options_scope_init(options_scope, locals_count);
102+
if (!pm_options_scope_init(options_scope, locals_count)) {
103+
rb_raise(rb_eNoMemError, "failed to allocate memory");
104+
}
101105

102106
// Iterate over the locals and add them to the scope.
103107
for (size_t local_index = 0; local_index < locals_count; local_index++) {

‎include/prism/options.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ PRISM_EXPORTED_FUNCTION bool pm_options_version_set(pm_options_t *options, const
129129
*
130130
* @param options The options struct to initialize the scopes array on.
131131
* @param scopes_count The number of scopes to allocate.
132+
* @return Whether or not the scopes array was initialized successfully.
132133
*/
133-
PRISM_EXPORTED_FUNCTION void pm_options_scopes_init(pm_options_t *options, size_t scopes_count);
134+
PRISM_EXPORTED_FUNCTION bool pm_options_scopes_init(pm_options_t *options, size_t scopes_count);
134135

135136
/**
136137
* Return a pointer to the scope at the given index within the given options.
@@ -147,8 +148,9 @@ PRISM_EXPORTED_FUNCTION const pm_options_scope_t * pm_options_scope_get(const pm
147148
*
148149
* @param scope The scope struct to initialize.
149150
* @param locals_count The number of locals to allocate.
151+
* @return Whether or not the scope was initialized successfully.
150152
*/
151-
PRISM_EXPORTED_FUNCTION void pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count);
153+
PRISM_EXPORTED_FUNCTION bool pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count);
152154

153155
/**
154156
* Return a pointer to the local at the given index within the given scope.

‎include/prism/util/pm_constant_pool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const
186186
* @param length The length of the constant.
187187
* @return The id of the constant.
188188
*/
189-
pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const uint8_t *start, size_t length);
189+
pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length);
190190

191191
/**
192192
* Insert a constant into a constant pool from memory that is constant. Returns

‎src/diagnostic.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,14 @@ pm_diagnostic_list_append_format(pm_list_t *list, const uint8_t *start, const ui
389389
*/
390390
void
391391
pm_diagnostic_list_free(pm_list_t *list) {
392-
pm_list_node_t *node, *next;
392+
pm_diagnostic_t *node = (pm_diagnostic_t *) list->head;
393393

394-
for (node = list->head; node != NULL; node = next) {
395-
next = node->next;
396-
pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) node;
394+
while (node != NULL) {
395+
pm_diagnostic_t *next = (pm_diagnostic_t *) node->node.next;
397396

398-
if (diagnostic->owned) free((void *) diagnostic->message);
399-
free(diagnostic);
397+
if (node->owned) free((void *) node->message);
398+
free(node);
399+
400+
node = next;
400401
}
401402
}

‎src/options.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,22 @@ pm_options_version_set(pm_options_t *options, const char *version, size_t length
6464
return false;
6565
}
6666

67+
// For some reason, GCC analyzer thinks we're leaking allocated scopes and
68+
// locals here, even though we definitely aren't. This is a false positive.
69+
// Ideally we wouldn't need to suppress this.
70+
#if defined(__GNUC__) && (__GNUC__ >= 10)
71+
#pragma GCC diagnostic push
72+
#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
73+
#endif
74+
6775
/**
6876
* Allocate and zero out the scopes array on the given options struct.
6977
*/
70-
PRISM_EXPORTED_FUNCTION void
78+
PRISM_EXPORTED_FUNCTION bool
7179
pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
7280
options->scopes_count = scopes_count;
7381
options->scopes = calloc(scopes_count, sizeof(pm_options_scope_t));
74-
if (options->scopes == NULL) abort();
82+
return options->scopes != NULL;
7583
}
7684

7785
/**
@@ -86,11 +94,11 @@ pm_options_scope_get(const pm_options_t *options, size_t index) {
8694
* Create a new options scope struct. This will hold a set of locals that are in
8795
* scope surrounding the code that is being parsed.
8896
*/
89-
PRISM_EXPORTED_FUNCTION void
97+
PRISM_EXPORTED_FUNCTION bool
9098
pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
9199
scope->locals_count = locals_count;
92100
scope->locals = calloc(locals_count, sizeof(pm_string_t));
93-
if (scope->locals == NULL) abort();
101+
return scope->locals != NULL;
94102
}
95103

96104
/**
@@ -192,14 +200,17 @@ pm_options_read(pm_options_t *options, const char *data) {
192200
data += 4;
193201

194202
if (scopes_count > 0) {
195-
pm_options_scopes_init(options, scopes_count);
203+
if (!pm_options_scopes_init(options, scopes_count)) return;
196204

197205
for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
198206
uint32_t locals_count = pm_options_read_u32(data);
199207
data += 4;
200208

201209
pm_options_scope_t *scope = &options->scopes[scope_index];
202-
pm_options_scope_init(scope, locals_count);
210+
if (!pm_options_scope_init(scope, locals_count)) {
211+
pm_options_free(options);
212+
return;
213+
}
203214

204215
for (size_t local_index = 0; local_index < locals_count; local_index++) {
205216
uint32_t local_length = pm_options_read_u32(data);
@@ -211,3 +222,7 @@ pm_options_read(pm_options_t *options, const char *data) {
211222
}
212223
}
213224
}
225+
226+
#if defined(__GNUC__) && (__GNUC__ >= 10)
227+
#pragma GCC diagnostic pop
228+
#endif

‎src/prism.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ pm_parser_constant_id_location(pm_parser_t *parser, const uint8_t *start, const
600600
* Retrieve the constant pool id for the given string.
601601
*/
602602
static inline pm_constant_id_t
603-
pm_parser_constant_id_owned(pm_parser_t *parser, const uint8_t *start, size_t length) {
603+
pm_parser_constant_id_owned(pm_parser_t *parser, uint8_t *start, size_t length) {
604604
return pm_constant_pool_insert_owned(&parser->constant_pool, start, length);
605605
}
606606

@@ -6409,7 +6409,7 @@ pm_parser_local_add_token(pm_parser_t *parser, pm_token_t *token) {
64096409
* Add a local variable from an owned string to the current scope.
64106410
*/
64116411
static pm_constant_id_t
6412-
pm_parser_local_add_owned(pm_parser_t *parser, const uint8_t *start, size_t length) {
6412+
pm_parser_local_add_owned(pm_parser_t *parser, uint8_t *start, size_t length) {
64136413
pm_constant_id_t constant_id = pm_parser_constant_id_owned(parser, start, length);
64146414
if (constant_id != 0) pm_parser_local_add(parser, constant_id);
64156415
return constant_id;
@@ -16945,7 +16945,7 @@ parse_regular_expression_named_captures(pm_parser_t *parser, const pm_string_t *
1694516945
memcpy(memory, source, length);
1694616946
// This silences clang analyzer warning about leak of memory pointed by `memory`.
1694716947
// NOLINTNEXTLINE(clang-analyzer-*)
16948-
name = pm_parser_constant_id_owned(parser, (const uint8_t *) memory, length);
16948+
name = pm_parser_constant_id_owned(parser, (uint8_t *) memory, length);
1694916949

1695016950
if (pm_token_is_numbered_parameter(source, source + length)) {
1695116951
const pm_location_t *location = &call->receiver->location;
@@ -18021,11 +18021,11 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm
1802118021
const uint8_t *source = pm_string_source(local);
1802218022
size_t length = pm_string_length(local);
1802318023

18024-
uint8_t *allocated = malloc(length);
18024+
void *allocated = malloc(length);
1802518025
if (allocated == NULL) continue;
1802618026

18027-
memcpy((void *) allocated, source, length);
18028-
pm_parser_local_add_owned(parser, allocated, length);
18027+
memcpy(allocated, source, length);
18028+
pm_parser_local_add_owned(parser, (uint8_t *) allocated, length);
1802918029
}
1803018030
}
1803118031
}
@@ -18235,8 +18235,9 @@ typedef struct {
1823518235
static inline pm_error_t *
1823618236
pm_parser_errors_format_sort(const pm_parser_t *parser, const pm_list_t *error_list, const pm_newline_list_t *newline_list) {
1823718237
pm_error_t *errors = calloc(error_list->size, sizeof(pm_error_t));
18238-
int32_t start_line = parser->start_line;
18238+
if (errors == NULL) return NULL;
1823918239

18240+
int32_t start_line = parser->start_line;
1824018241
for (pm_diagnostic_t *error = (pm_diagnostic_t *) error_list->head; error != NULL; error = (pm_diagnostic_t *) error->node.next) {
1824118242
pm_line_column_t start = pm_newline_list_line_column(newline_list, error->location.start, start_line);
1824218243
pm_line_column_t end = pm_newline_list_line_column(newline_list, error->location.end, start_line);
@@ -18316,7 +18317,9 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
1831618317
// insertion sort into a newly allocated array.
1831718318
const int32_t start_line = parser->start_line;
1831818319
const pm_newline_list_t *newline_list = &parser->newline_list;
18320+
1831918321
pm_error_t *errors = pm_parser_errors_format_sort(parser, error_list, newline_list);
18322+
if (errors == NULL) return;
1832018323

1832118324
// Now we're going to determine how we're going to format line numbers and
1832218325
// blank lines based on the maximum number of digits in the line numbers

‎src/util/pm_buffer.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pm_buffer_length(const pm_buffer_t *buffer) {
4747
/**
4848
* Append the given amount of space to the buffer.
4949
*/
50-
static inline void
50+
static inline bool
5151
pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
5252
size_t next_length = buffer->length + length;
5353

@@ -61,9 +61,11 @@ pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
6161
}
6262

6363
buffer->value = realloc(buffer->value, buffer->capacity);
64+
if (buffer->value == NULL) return false;
6465
}
6566

6667
buffer->length = next_length;
68+
return true;
6769
}
6870

6971
/**
@@ -72,8 +74,9 @@ pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
7274
static inline void
7375
pm_buffer_append(pm_buffer_t *buffer, const void *source, size_t length) {
7476
size_t cursor = buffer->length;
75-
pm_buffer_append_length(buffer, length);
76-
memcpy(buffer->value + cursor, source, length);
77+
if (pm_buffer_append_length(buffer, length)) {
78+
memcpy(buffer->value + cursor, source, length);
79+
}
7780
}
7881

7982
/**
@@ -82,8 +85,9 @@ pm_buffer_append(pm_buffer_t *buffer, const void *source, size_t length) {
8285
void
8386
pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length) {
8487
size_t cursor = buffer->length;
85-
pm_buffer_append_length(buffer, length);
86-
memset(buffer->value + cursor, 0, length);
88+
if (pm_buffer_append_length(buffer, length)) {
89+
memset(buffer->value + cursor, 0, length);
90+
}
8791
}
8892

8993
/**
@@ -100,13 +104,12 @@ pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) {
100104
size_t length = (size_t) (result + 1);
101105

102106
size_t cursor = buffer->length;
103-
pm_buffer_append_length(buffer, length);
104-
105-
va_start(arguments, format);
106-
vsnprintf(buffer->value + cursor, length, format, arguments);
107-
va_end(arguments);
108-
109-
buffer->length--;
107+
if (pm_buffer_append_length(buffer, length)) {
108+
va_start(arguments, format);
109+
vsnprintf(buffer->value + cursor, length, format, arguments);
110+
va_end(arguments);
111+
buffer->length--;
112+
}
110113
}
111114

112115
/**
@@ -236,9 +239,10 @@ pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t lengt
236239
void
237240
pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length) {
238241
size_t cursor = buffer->length;
239-
pm_buffer_append_length(buffer, length);
240-
memmove(buffer->value + length, buffer->value, cursor);
241-
memcpy(buffer->value, value, length);
242+
if (pm_buffer_append_length(buffer, length)) {
243+
memmove(buffer->value + length, buffer->value, cursor);
244+
memcpy(buffer->value, value, length);
245+
}
242246
}
243247

244248
/**

‎src/util/pm_constant_pool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, s
287287
* potential calls to resize fail.
288288
*/
289289
pm_constant_id_t
290-
pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
290+
pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length) {
291291
return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED);
292292
}
293293

‎src/util/pm_newline_list.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor) {
3030

3131
list->capacity = (list->capacity * 3) / 2;
3232
list->offsets = (size_t *) calloc(list->capacity, sizeof(size_t));
33+
if (list->offsets == NULL) return false;
34+
3335
memcpy(list->offsets, original_offsets, list->size * sizeof(size_t));
3436
free(original_offsets);
35-
if (list->offsets == NULL) return false;
3637
}
3738

3839
assert(*cursor == '\n');

0 commit comments

Comments
 (0)