diff options
-rw-r--r-- | firmware/buflib.c | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c index 52d52459a4..2a4b4b4f14 100644 --- a/firmware/buflib.c +++ b/firmware/buflib.c | |||
@@ -317,6 +317,20 @@ void handle_free(struct buflib_context *ctx, union buflib_data *handle) | |||
317 | ctx->compact = false; | 317 | ctx->compact = false; |
318 | } | 318 | } |
319 | 319 | ||
320 | static inline | ||
321 | union buflib_data* handle_to_block_end(struct buflib_context *ctx, int handle) | ||
322 | { | ||
323 | void *ptr = buflib_get_data(ctx, handle); | ||
324 | |||
325 | /* this is a valid case for shrinking if handle | ||
326 | * was freed by the shrink callback */ | ||
327 | if (!ptr) | ||
328 | return NULL; | ||
329 | |||
330 | union buflib_data *data = ALIGN_DOWN(ptr, sizeof(*data)); | ||
331 | return data; | ||
332 | } | ||
333 | |||
320 | /* Get the start block of an allocation */ | 334 | /* Get the start block of an allocation */ |
321 | static inline | 335 | static inline |
322 | union buflib_data* handle_to_block(struct buflib_context* ctx, int handle) | 336 | union buflib_data* handle_to_block(struct buflib_context* ctx, int handle) |
@@ -1038,7 +1052,7 @@ buflib_shrink(struct buflib_context* ctx, int handle, void* new_start, size_t ne | |||
1038 | 1052 | ||
1039 | const char* buflib_get_name(struct buflib_context *ctx, int handle) | 1053 | const char* buflib_get_name(struct buflib_context *ctx, int handle) |
1040 | { | 1054 | { |
1041 | union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data)); | 1055 | union buflib_data *data = handle_to_block_end(ctx, handle); |
1042 | size_t len = data[-bidx_BSIZE].val; | 1056 | size_t len = data[-bidx_BSIZE].val; |
1043 | if (len <= BUFLIB_NUM_FIELDS) | 1057 | if (len <= BUFLIB_NUM_FIELDS) |
1044 | return NULL; | 1058 | return NULL; |