-
Notifications
You must be signed in to change notification settings - Fork 16
Fix exception when decoded value is too large by doubling buffer #4
base: master
Are you sure you want to change the base?
Fix exception when decoded value is too large by doubling buffer #4
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for slow response.
Overall looks good, mostly trivial things to change except the behaviour when the delta is bigger than the input where I disagree with you.
output_buf = main_malloc(output_alloc); | ||
result = xd3_encode_memory(input_bytes, input_size, source_bytes, source_size, | ||
output_buf, &output_size, output_alloc, flags); | ||
} else { | ||
// output shouldn't be bigger than the original plus the delta, but give a little leeway | ||
output_alloc = input_size + source_size * 11 / 10; | ||
output_alloc = (input_size + source_size * 11 / 10) << buffer_double_cnt; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove comments that no longer make sense.
@@ -21,13 +21,13 @@ static PyObject * xdelta3_execute(PyObject *self, PyObject *args) | |||
|
|||
if (action == 0) { | |||
// if the output would be longer than the input itself, there's no point using delta encoding | |||
output_alloc = input_size; | |||
output_alloc = input_size << buffer_double_cnt; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid I don't agree with this. Sorry.
If I'm getting a delta that is bigger than the input I want to get an error which I have to deal with as soon as possible. Silently "failing" to produce a useful delta is not good.
Please remove this.
@@ -57,6 +51,14 @@ def test_version(): | |||
xdelta3.print_version() | |||
|
|||
|
|||
def test_huge_output(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes much more sense 😄
if (result == ENOSPC) { | ||
// Leave some leeway before we overflow int. This is a bad way, the better way would be to | ||
// check above when we left shift. | ||
if (buffer_double_cnt < sizeof(int) - 4) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you at least add a comment explaining what this is doing please.
} | ||
} else { | ||
char exc_str[80]; | ||
sprintf(exc_str, "Error occur executing xdelta3: %s", xd3_strerror(result)); | ||
snprintf(exc_str, sizeof(exc_str) / sizeof(exc_str[0]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense.
@karamanolev do you want to complete this? |
@samuelcolvin I'll address these soon, hopefully I'll find some time. Sorry. |
No problems, thank you. |
sizeof(int) - 4
is horrible, but I'm too lazy ATM to implement the correct solution, which is to check that you didn't overflow the int when left shifting in those 2 places. The error handling logic will be more complicated as well.