Fixed a few memory leaks.

This commit is contained in:
Ignacio 2024-04-01 12:39:27 -03:00
parent c0e5bc4a5b
commit d91b159ae1

View File

@ -123,6 +123,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
fprintf(stderr,ERR"Error reading from file.\n");
fclose(inFile);
fclose(outFile);
free(decrypted);
return 1;
}
fclose(inFile);
@ -145,6 +146,8 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
unsigned char* outputBuffer = malloc(totalSize);
if (outputBuffer == NULL) {
fprintf(stderr,ERR"Memory allocation error.\n");
free(decrypted);
fclose(outFile);
return 1;
}
memcpy(outputBuffer + offset, "gneurshk", strlen("gneurshk"));
@ -268,6 +271,7 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
if (memcmp(fullInput,"gneurshk",strlen("gneurshk")) != 0) {
fprintf(stderr,ERR"Invalid header.\n");
fprintf(stderr,ERR"Make sure the input file was signed by this program!\n");
free(fullInput);
fclose(outFile);
return 1;
}
@ -289,6 +293,8 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
unsigned char key[KEY_SIZE];
if (!keyGen(salt,password,key)) {
free(fullInput);
fclose(outFile);
return 1;
}
@ -299,17 +305,20 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
if (crypto_secretbox_open_easy(decrypted,encrypted,encLen,nonce,key) < 0) {
fprintf(stderr,ERR"Error decrypting file.\n");
free(fullInput);
fclose(outFile);
return 1;
}
if (fwrite(decrypted,1,decLen,outFile) != decLen) {
fprintf(stderr,ERR"Error writing data to file.\n");
free(fullInput);
fclose(outFile);
return 1;
}
fclose(outFile);
free(fullInput);
fprintf(stderr,"\n"OK"File decrypted!\n");