AmendHub

Download:

jcs

/

subtext

/

amendments

/

450

folder: Fix fread args to calculate checksums properly

fread returns the number of nmemb objects read, which was 1 (of size
1024), so we were only passing a size of 1 to SHA1Update. Read 1024
objects of size 1, to get a return value of 1024 to pass to SHA1.
 
Verify that the total we passed to SHA1Update is the full size of the
file, which we should have been doing anyuway.

jcs made amendment 450 about 1 year ago
--- folder.c Thu Mar 23 22:48:07 2023 +++ folder.c Mon Mar 27 11:06:25 2023 @@ -344,7 +344,7 @@ folder_upload(struct session *s, struct folder *folder struct stat sb; struct zmodem_session *zs; char *upload_path = NULL, *data = NULL, *tmp = NULL, *errorstr = NULL; - size_t size; + size_t size, rsize; short c, n, error; if (!s->user) { @@ -469,8 +469,10 @@ folder_upload(struct session *s, struct folder *folder session_flush(s); SHA1Init(&sha1); fp = fopen(upload_path, "rb"); + rsize = 0; for (n = 1; fp && !feof(fp); n++) { - size = fread(data, 1024, 1, fp); + size = fread(data, 1, 1024, fp); + rsize += size; SHA1Update(&sha1, (const u_int8_t *)data, size); if (n % 2 == 0) uthread_yield(); @@ -479,6 +481,13 @@ folder_upload(struct session *s, struct folder *folder if (fp) fclose(fp); xfree(&data); + + if (rsize != file.size) { + session_printf(s, "failed reading file! (%lu vs %lu)\r\n", + rsize, file.size); + session_flush(s); + goto file_upload_cancel; + } session_printf(s, "done.\r\n\r\n"); session_flush(s);