From e5fe638c68ca403007364b0c9580b25ab7435402 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 5 Oct 2022 09:33:18 +0200 Subject: [PATCH] Fix related with reallocation under some optimization situations. Signed-off-by: Pol Henarejos --- src/fs/file.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/fs/file.c b/src/fs/file.c index 645cca3..4b01bed 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -395,10 +395,17 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { uint8_t *tpos = p-asn1_len_tag(tag, tag_len); memmove(tpos, p, fdata+ef_size-p); tpos += fdata+ef_size-p; - uintptr_t meta_offset = tpos-fdata; + volatile uintptr_t meta_offset = tpos-fdata; ef_size += len - (tag_len-2); - if (len > tag_len-2) - fdata = (uint8_t *)realloc(fdata, ef_size); + if (len > tag_len-2) { + uint8_t *fdata_new = (uint8_t *)realloc(fdata, ef_size); + if (fdata_new != NULL) + fdata = fdata_new; + else { + free(fdata); + return CCID_ERR_MEMORY_FATAL; + } + } uint8_t *f = fdata+meta_offset; *f++ = fid & 0xff; f += format_tlv_len(len+2, f);