Merge pull request #41 from lioncash/leak

Fix a few minor memory leaks
This commit is contained in:
SciresM 2018-08-11 18:54:48 -07:00 committed by GitHub
commit 1371815e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 48 deletions

6
kip.c
View File

@ -273,10 +273,15 @@ void kip1_save(kip1_ctx_t *ctx) {
return;
}
const char *json = kip1_get_json(ctx);
if (json == NULL) {
fprintf(stderr, "Failed to allocate KIP1 JSON\n");
exit(EXIT_FAILURE);
}
if (fwrite(json, 1, strlen(json), f_json) != strlen(json)) {
fprintf(stderr, "Failed to write JSON file!\n");
exit(EXIT_FAILURE);
}
cJSON_free(json);
fclose(f_json);
} else if (uncmp_path->valid == VALIDITY_VALID) {
FILE *f_uncmp = os_fopen(uncmp_path->os_path, OS_MODE_WRITE);
@ -290,6 +295,7 @@ void kip1_save(kip1_ctx_t *ctx) {
fprintf(stderr, "Failed to write uncompressed kip!\n");
exit(EXIT_FAILURE);
}
free(uncmp);
fclose(f_uncmp);
}
}

76
nax0.c
View File

@ -108,43 +108,47 @@ void nax0_save(nax0_ctx_t *ctx) {
/* Save Decrypted Contents. */
filepath_t *dec_path = &ctx->tool_ctx->settings.plaintext_path;
if (dec_path->valid == VALIDITY_VALID) {
printf("Saving Decrypted NAX0 Content to %s...\n", dec_path->char_path);
FILE *f_dec = os_fopen(dec_path->os_path, OS_MODE_WRITE);
if (f_dec != NULL) {
uint64_t ofs = 0x4000;
uint64_t end_ofs = ofs + ctx->header.size;
unsigned char *buf = malloc(0x400000);
if (buf == NULL) {
fprintf(stderr, "Failed to allocate file-save buffer!\n");
exit(EXIT_FAILURE);
}
uint64_t read_size = 0x400000; /* 4 MB buffer. */
memset(buf, 0xCC, read_size); /* Debug in case I fuck this up somehow... */
while (ofs < end_ofs) {
if (ofs + read_size >= end_ofs) read_size = end_ofs - ofs;
if (nax0_read(ctx, ofs, buf, read_size) != read_size) {
fprintf(stderr, "Failed to read file!\n");
exit(EXIT_FAILURE);
}
uint64_t dec_size = (read_size + 0x3FFF) & ~0x3FFF;
aes_xts_decrypt(ctx->aes_ctx, buf, buf, dec_size, (ofs - 0x4000) >> 14, 0x4000);
if (fwrite(buf, 1, read_size, f_dec) != read_size) {
fprintf(stderr, "Failed to write file!\n");
exit(EXIT_FAILURE);
}
ofs += read_size;
}
free(buf);
} else {
fprintf(stderr, "Failed to open %s!\n", dec_path->char_path);
}
if (dec_path->valid != VALIDITY_VALID) {
return
}
printf("Saving Decrypted NAX0 Content to %s...\n", dec_path->char_path);
FILE *f_dec = os_fopen(dec_path->os_path, OS_MODE_WRITE);
if (f_dec == NULL) {
fprintf(stderr, "Failed to open %s!\n", dec_path->char_path);
return;
}
uint64_t ofs = 0x4000;
uint64_t end_ofs = ofs + ctx->header.size;
unsigned char *buf = malloc(0x400000);
if (buf == NULL) {
fprintf(stderr, "Failed to allocate file-save buffer!\n");
exit(EXIT_FAILURE);
}
uint64_t read_size = 0x400000; /* 4 MB buffer. */
memset(buf, 0xCC, read_size); /* Debug in case I fuck this up somehow... */
while (ofs < end_ofs) {
if (ofs + read_size >= end_ofs) read_size = end_ofs - ofs;
if (nax0_read(ctx, ofs, buf, read_size) != read_size) {
fprintf(stderr, "Failed to read file!\n");
exit(EXIT_FAILURE);
}
uint64_t dec_size = (read_size + 0x3FFF) & ~0x3FFF;
aes_xts_decrypt(ctx->aes_ctx, buf, buf, dec_size, (ofs - 0x4000) >> 14, 0x4000);
if (fwrite(buf, 1, read_size, f_dec) != read_size) {
fprintf(stderr, "Failed to write file!\n");
exit(EXIT_FAILURE);
}
ofs += read_size;
}
fclose(f_dec);
free(buf);
}
const char *nax0_get_key_summary(unsigned int k) {

28
npdm.c
View File

@ -662,19 +662,23 @@ void npdm_print(npdm_t *npdm, hactool_ctx_t *tool_ctx) {
void npdm_save(npdm_t *npdm, hactool_ctx_t *tool_ctx) {
filepath_t *json_path = &tool_ctx->settings.npdm_json_path;
if (json_path->valid == VALIDITY_VALID) {
FILE *f_json = os_fopen(json_path->os_path, OS_MODE_WRITE);
if (f_json == NULL) {
fprintf(stderr, "Failed to open %s!\n", json_path->char_path);
return;
}
const char *json = npdm_get_json(npdm);
if (fwrite(json, 1, strlen(json), f_json) != strlen(json)) {
fprintf(stderr, "Failed to write JSON file!\n");
exit(EXIT_FAILURE);
}
fclose(f_json);
if (json_path->valid != VALIDITY_VALID) {
return;
}
FILE *f_json = os_fopen(json_path->os_path, OS_MODE_WRITE);
if (f_json == NULL) {
fprintf(stderr, "Failed to open %s!\n", json_path->char_path);
return;
}
const char *json = npdm_get_json(npdm);
if (fwrite(json, 1, strlen(json), f_json) != strlen(json)) {
fprintf(stderr, "Failed to write JSON file!\n");
exit(EXIT_FAILURE);
}
cJSON_free(json);
fclose(f_json);
}
void cJSON_AddU8ToObject(cJSON *obj, char *name, uint8_t val) {