Skip to content

Commit

Permalink
Handled memory reallocation error
Browse files Browse the repository at this point in the history
  • Loading branch information
olszomal authored and mtrojnar committed Jun 3, 2024
1 parent 771014a commit 3c8c74a
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions msi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1499,15 +1499,20 @@ static int msi_hash_dir(MSI_FILE *msi, MSI_DIRENT *dirent, BIO *hash, int is_roo
return ret;
}

static void ministream_append(MSI_OUT *out, char *buf, uint32_t len)
static int ministream_append(MSI_OUT *out, char *buf, uint32_t len)
{
uint32_t needSectors = (len + out->sectorSize - 1) / out->sectorSize;
if (out->miniStreamLen + len >= (uint64_t)out->ministreamsMemallocCount * out->sectorSize) {
out->ministreamsMemallocCount += needSectors;
out->ministream = OPENSSL_realloc(out->ministream, (size_t)(out->ministreamsMemallocCount * out->sectorSize));
if (!out->ministream) {
printf("Memory allocation failure\n");
return 0; /* FAILED */
}
}
memcpy(out->ministream + out->miniStreamLen, buf, (size_t)len);
out->miniStreamLen += len;
return 1; /* OK */
}

static int minifat_append(MSI_OUT *out, char *buf, uint32_t len)
Expand All @@ -1519,6 +1524,10 @@ static int minifat_append(MSI_OUT *out, char *buf, uint32_t len)
return 0; /* FAILED */
}
out->minifat = OPENSSL_realloc(out->minifat, (size_t)(out->minifatMemallocCount * out->sectorSize));
if (!out->minifat) {
printf("Memory allocation failure\n");
return 0; /* FAILED */
}
}
memcpy(out->minifat + out->minifatLen, buf, (size_t)len);
out->minifatLen += len;
Expand All @@ -1534,6 +1543,10 @@ static int fat_append(MSI_OUT *out, char *buf, uint32_t len)
return 0; /* FAILED */
}
out->fat = OPENSSL_realloc(out->fat, (size_t)(out->fatMemallocCount * out->sectorSize));
if (!out->fat) {
printf("Memory allocation failure\n");
return 0; /* FAILED */
}
}
memcpy(out->fat + out->fatLen, buf, (size_t)len);
out->fatLen += len;
Expand All @@ -1549,6 +1562,10 @@ static int difat_append(MSI_OUT *out, char *buf, uint32_t len)
return 0; /* FAILED */
}
out->difat = OPENSSL_realloc(out->difat, (size_t)(out->difatMemallocCount * out->sectorSize));
if (!out->difat) {
printf("Memory allocation failure\n");
return 0; /* FAILED */
}
}
memcpy(out->difat + out->difatLen, buf, (size_t)len);
out->difatLen += len;
Expand Down Expand Up @@ -1696,12 +1713,18 @@ static int stream_handle(MSI_FILE *msi, MSI_DIRENT *dirent, u_char *p_msi, uint3
if (inlen < MINI_STREAM_CUTOFF_SIZE) {
/* set the index into the mini FAT to track the chain of sectors through the mini stream */
child->entry->startSectorLocation = out->miniSectorNum;
ministream_append(out, indata, inlen);
if (!ministream_append(out, indata, inlen)) {
OPENSSL_free(indata);
return 0; /* FAILED */
}
/* fill to the end with known data, such as all zeroes */
if (inlen % msi->m_minisectorSize > 0) {
uint32_t remain = msi->m_minisectorSize - inlen % msi->m_minisectorSize;
memset(buf, 0, (size_t)remain);
ministream_append(out, buf, remain);
if (!ministream_append(out, buf, remain)) {
OPENSSL_free(indata);
return 0; /* FAILED */
}
}
while (inlen > msi->m_minisectorSize) {
out->miniSectorNum++;
Expand Down

0 comments on commit 3c8c74a

Please sign in to comment.