tls: Improve certificat saving - sacc - sacc(omys), simple console gopher client | |
git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65… | |
Log | |
Files | |
Refs | |
Tags | |
LICENSE | |
--- | |
commit 0fce134e1baa2ae47aeb0ee7c091e6c25651bcea | |
parent 314af1528acc78f62256043cb8a9c81f8f7833b8 | |
Author: Quentin Rameau <[email protected]> | |
Date: Fri, 3 May 2024 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. | |
Diffstat: | |
M io_tls.c | 14 +++++++++++--- | |
1 file changed, 11 insertions(+), 3 deletions(-) | |
--- | |
diff --git 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 * |