Fixed a few memory leaks.

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

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