feat: safer `free()'s

`free' allocated buffers only on success inside the main loop, and
`free' them on error inside their respective functions

(fixes an UB where an already freed buffer is freed again in the main
loop)
This commit is contained in:
Tom MTT 2022-11-23 07:08:13 +01:00
parent be74fa43cc
commit 6c57a4d03c
2 changed files with 12 additions and 6 deletions

View file

@ -340,6 +340,8 @@ int main(int argc, char *argv[])
send_response(connection, url);
verbose(1, "All done.");
free(url);
} else {
error("error while making a valid URL.");
send_response(connection, "Could not create your paste URL.\nPlease try again later.\n");
@ -348,10 +350,14 @@ int main(int argc, char *argv[])
error("error while writing paste to disk.");
send_response(connection, "Could not write your paste to disk.\nPlease try again later.\n");
}
free(id);
} else {
error("error while generating a random ID.");
send_response(connection, "Could not generate your paste ID.\nPlease try again later.\n");
}
free(paste);
} else {
if (errno == EFBIG)
send_response(connection, "Paste too big.\n");
@ -365,11 +371,6 @@ int main(int argc, char *argv[])
error("error %d while reading paste from incoming connection.", errno);
}
/* free resources */
free(paste);
free(id);
free(url);
/* close connection */
close_connection(connection);
}