Commit 1e75a76742dd80fc5d51247d76da89fadd50f9eb
1 parent
9e0f8c47
more robust template iteration
Showing
1 changed file
with
14 additions
and
4 deletions
openbr/universal_template.cpp
| ... | ... | @@ -52,7 +52,7 @@ static void callAndFree(br_utemplate_callback callback, br_utemplate t, br_callb |
| 52 | 52 | free(t); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | -static bool read_buffer(FILE *file, char *buffer, size_t bytes, bool eofAllowed) | |
| 55 | +static bool read_buffer(FILE *file, char *buffer, size_t bytes) | |
| 56 | 56 | { |
| 57 | 57 | size_t bytesRemaining = bytes; |
| 58 | 58 | while (bytesRemaining) { |
| ... | ... | @@ -61,7 +61,9 @@ static bool read_buffer(FILE *file, char *buffer, size_t bytes, bool eofAllowed) |
| 61 | 61 | bytesRemaining -= bytesRead; |
| 62 | 62 | |
| 63 | 63 | if (feof(file)) { |
| 64 | - if (eofAllowed && (bytesRemaining == bytes)) | |
| 64 | + if ((bytesRemaining != bytes) && !fseek(file, bytesRemaining - bytes, SEEK_CUR)) // Try to rewind | |
| 65 | + bytesRemaining = bytes; | |
| 66 | + if (bytesRemaining == bytes) | |
| 65 | 67 | return false; |
| 66 | 68 | qFatal("End of file after reading %d of %d bytes.", int(bytes - bytesRemaining), int(bytes)); |
| 67 | 69 | } |
| ... | ... | @@ -81,13 +83,21 @@ int br_iterate_utemplates_file(FILE *file, br_utemplate_callback callback, br_ca |
| 81 | 83 | while (true) { |
| 82 | 84 | br_utemplate t = (br_utemplate) malloc(sizeof(br_universal_template)); |
| 83 | 85 | |
| 84 | - if (!read_buffer(file, (char*) t, sizeof(br_universal_template), true)) { | |
| 86 | + if (!read_buffer(file, (char*) t, sizeof(br_universal_template))) { | |
| 85 | 87 | free(t); |
| 86 | 88 | break; |
| 87 | 89 | } |
| 88 | 90 | |
| 89 | 91 | t = (br_utemplate) realloc(t, sizeof(br_universal_template) + t->urlSize + t->fvSize); |
| 90 | - read_buffer(file, (char*) &t->data, t->urlSize + t->fvSize, false); | |
| 92 | + if (!read_buffer(file, (char*) &t->data, t->urlSize + t->fvSize)) { | |
| 93 | + free(t); | |
| 94 | + | |
| 95 | + // Try to rewind header read | |
| 96 | + if (fseek(file, -sizeof(br_universal_template), SEEK_CUR)) | |
| 97 | + qFatal("Unable to recover from partial template read!"); | |
| 98 | + | |
| 99 | + break; | |
| 100 | + } | |
| 91 | 101 | |
| 92 | 102 | if (parallel) futures.addFuture(QtConcurrent::run(callAndFree, callback, t, context)); |
| 93 | 103 | else callAndFree(callback, t, context); | ... | ... |