Skip to content
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

Fixed bug in decoding large images #3791

Merged
merged 1 commit into from May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions Tests/check_large_memory.py
Expand Up @@ -12,6 +12,12 @@
# 2.7 and 3.2.

from PIL import Image

try:
import numpy
except ImportError:
numpy = None

YDIM = 32769
XDIM = 48000

Expand All @@ -32,6 +38,11 @@ def test_2gpx(self):
"""failed prepatch"""
self._write_png(XDIM, XDIM)

@unittest.skipIf(numpy is None, "Numpy is not installed")
def test_size_greater_than_int(self):
arr = numpy.ndarray(shape=(16394, 16394))
Image.fromarray(arr)


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion src/decode.c
Expand Up @@ -48,7 +48,7 @@
typedef struct {
PyObject_HEAD
int (*decode)(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
int (*cleanup)(ImagingCodecState state);
struct ImagingCodecStateInstance state;
Imaging im;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/BcnDecode.c
Expand Up @@ -844,7 +844,7 @@ static int decode_bcn(Imaging im, ImagingCodecState state, const UINT8* src, int
return (int)(ptr - src);
}

int ImagingBcnDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) {
int ImagingBcnDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes) {
int N = state->state & 0xf;
int width = state->xsize;
int height = state->ysize;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/BitDecode.c
Expand Up @@ -20,7 +20,7 @@


int
ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
BITSTATE* bitstate = state->context;
UINT8* ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/FliDecode.c
Expand Up @@ -26,7 +26,7 @@


int
ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
UINT8* ptr;
int framesize;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/GifDecode.c
Expand Up @@ -58,7 +58,7 @@


int
ImagingGifDecode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes)
ImagingGifDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_ssize_t bytes)
{
UINT8* p;
UINT8* out;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/HexDecode.c
Expand Up @@ -21,7 +21,7 @@
(v >= 'A' && v <= 'F') ? v - 'A' + 10 : -1)

int
ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
UINT8* ptr;
int a, b;
Expand Down
38 changes: 19 additions & 19 deletions src/libImaging/Imaging.h
Expand Up @@ -413,22 +413,22 @@ typedef int (*ImagingCodec)(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);

extern int ImagingBcnDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingBitDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingEpsEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
extern int ImagingFliDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingGifDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingGifEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
extern int ImagingHexDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
#ifdef HAVE_LIBJPEG
extern int ImagingJpegDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingJpegDecodeCleanup(ImagingCodecState state);
extern int ImagingJpegUseJCSExtensions(void);

Expand All @@ -437,52 +437,52 @@ extern int ImagingJpegEncode(Imaging im, ImagingCodecState state,
#endif
#ifdef HAVE_OPENJPEG
extern int ImagingJpeg2KDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingJpeg2KDecodeCleanup(ImagingCodecState state);
extern int ImagingJpeg2KEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
extern int ImagingJpeg2KEncodeCleanup(ImagingCodecState state);
#endif
#ifdef HAVE_LIBTIFF
extern int ImagingLibTiffDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingLibTiffEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
#endif
#ifdef HAVE_LIBMPEG
extern int ImagingMpegDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
#endif
extern int ImagingMspDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingPackbitsDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingPcdDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingPcxDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingPcxEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
extern int ImagingRawDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingRawEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
extern int ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingSgiRleDecodeCleanup(ImagingCodecState state);
extern int ImagingSunRleDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingTgaRleEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
extern int ImagingXbmDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingXbmEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
#ifdef HAVE_LIBZ
extern int ImagingZipDecode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
UINT8* buffer, Py_ssize_t bytes);
extern int ImagingZipDecodeCleanup(ImagingCodecState state);
extern int ImagingZipEncode(Imaging im, ImagingCodecState state,
UINT8* buffer, int bytes);
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/Jpeg2KDecode.c
Expand Up @@ -769,7 +769,7 @@ j2k_decode_entry(Imaging im, ImagingCodecState state)
}

int
ImagingJpeg2KDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingJpeg2KDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{

if (bytes){
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/JpegDecode.c
Expand Up @@ -144,7 +144,7 @@ output(j_common_ptr cinfo)
/* -------------------------------------------------------------------- */

int
ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
JPEGSTATE* context = (JPEGSTATE*) state->context;
int ok;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/PackDecode.c
Expand Up @@ -18,7 +18,7 @@

int
ImagingPackbitsDecode(Imaging im, ImagingCodecState state,
UINT8* buf, int bytes)
UINT8* buf, Py_ssize_t bytes)
{
UINT8 n;
UINT8* ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/PcdDecode.c
Expand Up @@ -24,7 +24,7 @@


int
ImagingPcdDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingPcdDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
int x;
int chunk;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/PcxDecode.c
Expand Up @@ -17,7 +17,7 @@
#include "Imaging.h"

int
ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
UINT8 n;
UINT8* ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/RawDecode.c
Expand Up @@ -20,7 +20,7 @@


int
ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
enum { LINE = 1, SKIP };
RAWSTATE* rawstate = state->context;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/SgiRleDecode.c
Expand Up @@ -90,7 +90,7 @@ static int expandrow2(UINT16* dest, UINT16* src, int n, int z)

int
ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
UINT8* buf, int bytes)
UINT8* buf, Py_ssize_t bytes)
{
UINT8 *ptr;
SGISTATE *c;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/SunRleDecode.c
Expand Up @@ -20,7 +20,7 @@


int
ImagingSunRleDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingSunRleDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
int n;
UINT8* ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/TgaRleDecode.c
Expand Up @@ -20,7 +20,7 @@

int
ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
UINT8* buf, int bytes)
UINT8* buf, Py_ssize_t bytes)
{
int n, depth;
UINT8* ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/TiffDecode.c
Expand Up @@ -275,7 +275,7 @@ int ReadStrip(TIFF* tiff, UINT32 row, UINT32* buffer) {
return 0;
}

int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes) {
int ImagingLibTiffDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_ssize_t bytes) {
TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
char *filename = "tempfile.tif";
char *mode = "r";
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/XbmDecode.c
Expand Up @@ -21,7 +21,7 @@
(v >= 'A' && v <= 'F') ? v - 'A' + 10 : 0)

int
ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
enum { BYTE = 1, SKIP };

Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/ZipDecode.c
Expand Up @@ -41,7 +41,7 @@ static int get_row_len(ImagingCodecState state, int pass)
/* -------------------------------------------------------------------- */

int
ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
ZIPSTATE* context = (ZIPSTATE*) state->context;
int err;
Expand Down