commit 0fce134e1baa2ae47aeb0ee7c091e6c25651bcea Quentin Rameau <quinq@fifth.space> 2024-05-03 06:12:10 +0200 tls: Improve certificat saving While the original code wouldn't pose a problem in practice, as we're not expecting a server to give us a certificate of over 1GB, this should make it a tad more robust.
io_tls.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/io_tls.c b/io_tls.c index cd71fc7..ad994da 100644 --- a/io_tls.c +++ b/io_tls.c @@ -106,6 +106,7 @@ savepem(struct tls *t, char *path) FILE *f; const char *s; size_t ln; + int e = 0; if (path == NULL) return -1; @@ -113,11 +114,18 @@ savepem(struct tls *t, char *path) return -1; if ((f = fopen(path, "w")) == NULL) return -1; - fprintf(f, "%.*s\n", ln, s); + + while (ln > 0) + ln = fwrite(s, 1, ln, f); + + if (ferror(f)) + e = -1; if (fclose(f) != 0) - return -1; + e = -1; + if (e == -1) + unlink(path); - return 0; + return e; } static char *