-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failure when compressing no data #57
Comments
As discussed, here's some more detail plus the proposed patch: $ git diff
diff --git a/src/heatshrink_encoder.c b/src/heatshrink_encoder.c
index 59088e48..ba2e4a39 100644
--- a/src/heatshrink_encoder.c
+++ b/src/heatshrink_encoder.c
@@ -254,7 +254,7 @@ HSE_finish_res heatshrink_encoder_finish(heatshrink_encoder *hse) {
if (hse == NULL) { return HSER_FINISH_ERROR_NULL; }
LOG("-- setting is_finishing flag\n");
hse->flags |= FLAG_IS_FINISHING;
- if (hse->state == HSES_NOT_FULL) { hse->state = HSES_FILLED; }
+ if (hse->state == HSES_NOT_FULL) { hse->state = hse->input_size ? HSES_FILLED : HSES_DONE; }
return hse->state == HSES_DONE ? HSER_FINISH_DONE : HSER_FINISH_MORE;
}
Also just tested this again with the demo encoder/decoder tool and this easily reproduces the issue too: touch emptyfile.txt
./heatshrink -e emptyfile.txt emptyfile.hs.bin When compiling this with
An alternatvie patch might be adding the I hope these information help with further assessment of the situation. |
This is going to go in a buxfix point release in the next few weeks, since the new features for 0.5.0 might take longer. Thanks for the detailed report. |
When calling
heatshrink_encoder_finish
directly afterheatshrink_encoder_reset
the subsequent call toheatshrink_encoder_poll
indicated by the return value of theheatshrink_encoder_finish
call fails withHSER_ERROR_MISUSE
. A more robust API should instead either returnHSER_FINISH_DONE
inheatshrink_encoder_finish
directly (there's no pending data to be written) or detect this situation inheatshrink_encoder_poll
and return without writing any output to the buffer.To demonstrate the issue:
Expected output:
Actual output:
The original code I used for compression (when I noticed the underlaying issue) is like this:
Which ran into a buffer overrun when no call to
compress_init
andcompress_finish
took place due to this unexpected behaviour of the heatshrink API.The text was updated successfully, but these errors were encountered: