Fix invalid pointer

This commit is contained in:
Ignacio 2024-04-01 20:59:58 -03:00
parent 274467fcf8
commit d3e3123373

24
salty.c
View File

@ -116,7 +116,6 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
// Read input file
fprintf(stderr,NOR"Reading input filesize...\n");
decLen = fileSize(inFile);
fprintf(stderr,OK"File is %d bytes.\n",decLen);
decrypted = malloc(decLen);
if (fread(decrypted,1,decLen,inFile) != decLen) {
@ -129,7 +128,9 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
fclose(inFile);
}
fprintf(stderr,OK"Input read successfully!\n");
size_t encLen = decLen + crypto_secretbox_MACBYTES;
fprintf(stderr,OK"File is %d bytes.\n",decLen);
fprintf(stderr,OK"Encoding file to %d bytes.\n", encLen);
fprintf(stderr,NOR"Beginning encryption...\n");
// Generate a nonce, write headers and encrypted message
@ -137,11 +138,11 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
randombytes_buf(nonce,sizeof(nonce));
// Encrypt the data
unsigned char encrypted[decLen + crypto_secretbox_MACBYTES];
unsigned char encrypted[encLen];
crypto_secretbox_easy(encrypted, decrypted, decLen, nonce, key);
// Patchy but correct-ish attempt to solve annoying stdout issue
size_t totalSize = strlen("gneurshk") + sizeof(salt) + sizeof(nonce) + sizeof(encrypted);
size_t totalSize = strlen("gneurshk") + sizeof(salt) + sizeof(nonce) + encLen;
size_t offset = 0;
unsigned char* outputBuffer = malloc(totalSize);
if (outputBuffer == NULL) {
@ -261,6 +262,12 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
size_t encLen = inLen - sizeof(salt) - sizeof(nonce) - 8;
if (inLen == 0) {
fprintf(stderr,ERR"File size is 0. No data.");
fclose(outFile);
return 1;
}
fprintf(stderr,DBG"Size of encrypted file: %d bytes.\n",inLen);
fprintf(stderr,DBG"This means the encrypted content is %d bytes.\n",encLen);
@ -430,10 +437,13 @@ int main(int argc, char *argv[]) {
char* password;
// Set password and run!
if (pass == -1) {
password = getpass("Enter your encryption password: ");
} else {
if (pass > 0) {
password = argv[pass];
} else if (in < 0) {
fprintf(stderr,ERR"Cannot read from stdin without a password.\n");
return 1;
} else {
password = getpass("Enter your encryption password: ");
}
if (decrypt) {