Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

fix for large decoded value #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ htmlcov/
/*.txt
/*.whl
/xdelta3/lib/
/tests/b1.bin
/tests/b2.bin
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ install:
- gcc -v
#- docker pull $DOCKER_IMAGE
- pip freeze
- make download-test-files

script:
- make test
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ lint:
flake8 xdelta3/ tests/
pytest xdelta3 -p no:sugar -q

.PHONY: download-test-files
download-test-files:
curl -sL https://github.com/samuelcolvin/xdelta3-python/files/1579377/files.zip > test_html.zip
echo "52bf4ee680a86afdeed9aad30e7d68fa test_html.zip" > test_html.zip.md5
md5sum -c test_html.zip.md5
unzip -o test_html.zip
mv b1.bin b2.bin tests
rm test_html.zip test_html.zip.md5

.PHONY: test
test: install
pytest --cov=xdelta3
Expand Down
14 changes: 14 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import os
import string
from pathlib import Path

import pytest

Expand Down Expand Up @@ -64,3 +65,16 @@ def test_readme():

value_two_rebuilt = xdelta3.decode(value_one, delta)
assert value_two_rebuilt == value_two


def test_large_decode():
this_dir = Path(__file__).parent
try:
b1 = (this_dir / 'b1.bin').read_bytes()
b2 = (this_dir / 'b2.bin').read_bytes()
except FileNotFoundError as e:
raise RuntimeError('file required for test not found, run `make download-test-files`') from e

d = xdelta3.encode(b1, b2)
b3 = xdelta3.decode(b1, d)
assert b2 == b3
22 changes: 12 additions & 10 deletions xdelta3/_xdelta3.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ static PyObject * xdelta3_execute(PyObject *self, PyObject *args)
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_buf = main_malloc(output_alloc);
result = xd3_decode_memory(input_bytes, input_size, source_bytes, source_size,
output_buf, &output_size, output_alloc, flags);
output_alloc = input_size + source_size;
while (1) {
output_buf = main_malloc(output_alloc);
result = xd3_decode_memory(input_bytes, input_size, source_bytes, source_size,
output_buf, &output_size, output_alloc, flags);
if (result != ENOSPC) {
break;
}
output_alloc = output_alloc * 2;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add main_free(output_buf); here to avoid leaking memory.

}
}

if (result == 0) {
Expand All @@ -40,12 +46,8 @@ static PyObject * xdelta3_execute(PyObject *self, PyObject *args)
}

if(result == ENOSPC) {
if (action == 0) {
// all is well, just not efficient delta could be found
PyErr_SetString(NoDeltaFound, "No delta found shorter than the input value");
} else {
PyErr_SetString(XDeltaError, "Output of decoding delta longer than expected");
}
// all is well, just no efficient delta could be found
PyErr_SetString(NoDeltaFound, "No delta found shorter than the input value");
} else {
char exc_str[80];
sprintf(exc_str, "Error occur executing xdelta3: %s", xd3_strerror(result));
Expand Down