diff --git a/rtengine/libraw/Changelog.txt b/rtengine/libraw/Changelog.txt index 50fb003b5f..d85edbef6a 100644 --- a/rtengine/libraw/Changelog.txt +++ b/rtengine/libraw/Changelog.txt @@ -1,19 +1,36 @@ +2024-09-18 Alex Tutubalin + + LibRaw 0.21.3-Release + * new compile-time define LIBRAW_CALLOC_RAWSTORE + If defined: calloc() will be used for all big buffer + allocations to prevent uninitialized heap data leak + + * Small allocations: calloc() instead of malloc() to prevent + uninitialized heap data leak + + * Support for 4-component JPEG-compressed DNG files + + * CR3-Qstep table: avoid wrong 64-bit code generation + + * Fixed several bugs related to specially-crafted files + processing (thanks to OSS-Fuzz project) + 2024-03-30 Alex Tutubalin LibRaw-snapshot-202403 - * Raw format support + * Raw format support 1) Panasonic encoding 8 - 2) Sony YCC (Medium/Small compression) pseudo-RAW support: + 2) Sony YCC (Medium/Small compression) pseudo-RAW support: -- normal processing (3-channel RGB output) -- or YCbCr output if LIBRAW_RAWSPECIAL_SRAW_NO_RGB bit is set - in imgdata.rawparams.specials (one may specify the + in imgdata.rawparams.specials (one may specify the LIBRAW_RAWSPECIAL_SRAW_NO_INTERPOLATE flag too avoid Cb/Cr interpolation) Note: if YCbCr is selected, Cb/Cr neutral points are adjusted to 8192 (to fit histogram/range area of the Y channel) - -- RAW (pseudo-RAW) data is already white balanced, so - imgdata.color.as_shot_wb_applied is set to + -- RAW (pseudo-RAW) data is already white balanced, so + imgdata.color.as_shot_wb_applied is set to LIBRAW_ASWB_APPLIED | LIBRAW_ASWB_SONY 3) DNG 1.7 (including JPEG-XL compression) support via Adobe DNG SDK 1.7.x integration @@ -30,7 +47,7 @@ - OM Digital OM-5 - Panasonic DC-G9 II, DC-ZS200D / ZS220D, DC-TZ200D / TZ202D / TZ220D, DC-S5-II, DC-GH6 - Sony A7C-II, A7CR, ILCE-6700, ZV-1M2, ZV-E1, ILCE-7RM5 (A7R-V), ILME-FX30, - A1: support for files damaged by exiftool + A1: support for files damaged by exiftool - Multiple DJI and Skydio drones - tested with multiple smartphones with DNG format recorded. @@ -39,19 +56,19 @@ * Better support for cropped images from latest Fujifilm cameras (X-H2, X-H2S, X-T5) - * As planned in LibRaw snapshot 202101 release notes, OLD_VIDEOCAMS + * As planned in LibRaw snapshot 202101 release notes, OLD_VIDEOCAMS support is dropped. These cameras are no longer supported: - old RED Cine cameras - Canon C500 - - ARRI video/digital cinema cameras + - ARRI video/digital cinema cameras * JPEG-XL previews (DNG 1.7) support: - new bit for imgdata.rawparams.options: LIBRAW_RAWOPTIONS_ALLOW_JPEGXL_PREVIEWS (not set by default) - if the bit is set, LibRaw will perceive such previews in the same way as JPEG ones (largest preview is selected by default, etc). - tformat field for such preview is set to LIBRAW_THUMBNAIL_JPEGXL - - image metadata (width/height/channels count) is not parsed for JPEG-XL previews + - image metadata (width/height/channels count) is not parsed for JPEG-XL previews * New patch for RawSpeed3: 05.no-phase-one-correction.patch this patch disables RawSpeed's PhaseOne flat field correction (not needed because @@ -77,13 +94,13 @@ * Canon CR2 16-bit bitmap previes: do not convert from Kodak colorspace - * New compile-defined limit LIBRAW_MAX_PROFILE_SIZE_MB: + * New compile-defined limit LIBRAW_MAX_PROFILE_SIZE_MB: limits allocation/read size for embedded color profile (default: 256Mb) * Embedded color profile allocation/read size: limited by input file size. * Check against corrupted LJPEG header in Canon sRAW decoder - + * Better support for Sony metadata * Better handling of memory allocation errors in unpack_thumb() @@ -107,9 +124,40 @@ * tiff parser: check for IFD offset before parsing it +2023-12-19 Alex Tutubalin + + LibRaw 0.21.2-Release + + * New compile-defined limit LIBRAW_MAX_PROFILE_SIZE_MB: + limits allocation/read size for embedded color profile (default: 256Mb) + + * Embedded color profile allocation/read size: limited by input file size. + + * Multiple fixes (mostly inspired by oss-fuzz) to improve library stability and/or input checks. + + * raw-identify: use fallback if PATH_MAX not available + + * Disabled color conversion for Canon 16-bit thumbnails + + * docs/changelog: explained the case when no thumbnail is found in specific file + + * swapXX renamed to libraw_swapXX to avoid name conflict + + * better striped thumbnails handling + + +2023-01-05 Alex Tutubalin + LibRaw 0.21.1-Release + * fixed typo in panasonic metadata parser + +2022-12-18 Alex Tutubalin + LibRaw 0.21-Release + + * Multiple fixes inspired by oss-fuzz project + 2022-07-01 Alex Tutubalin - LibRaw 0.21 + LibRaw 0.21-Beta1 == Camera format support == * Phase One/Leaf IIQ-S v2 support diff --git a/rtengine/libraw/libraw/libraw_version.h b/rtengine/libraw/libraw/libraw_version.h index 143a769ada..507571b88f 100644 --- a/rtengine/libraw/libraw/libraw_version.h +++ b/rtengine/libraw/libraw/libraw_version.h @@ -22,8 +22,8 @@ it under the terms of the one of two licenses as you choose: #define LIBRAW_MAJOR_VERSION 0 #define LIBRAW_MINOR_VERSION 21 -#define LIBRAW_PATCH_VERSION 0 -#define LIBRAW_VERSION_TAIL Beta1 +#define LIBRAW_PATCH_VERSION 3 +#define LIBRAW_VERSION_TAIL Release #define LIBRAW_SHLIB_CURRENT 23 #define LIBRAW_SHLIB_REVISION 0 diff --git a/rtengine/libraw/src/decoders/decoders_dcraw.cpp b/rtengine/libraw/src/decoders/decoders_dcraw.cpp index bd70b2dbd2..055f93b291 100644 --- a/rtengine/libraw/src/decoders/decoders_dcraw.cpp +++ b/rtengine/libraw/src/decoders/decoders_dcraw.cpp @@ -1025,7 +1025,7 @@ void LibRaw::nokia_load_raw() if (raw_stride) dwide = raw_stride; #endif - std::vector data(dwide * 2 + 4); + std::vector data(dwide * 2 + 4,0); for (row = 0; row < raw_height; row++) { checkCancel(); @@ -1066,11 +1066,17 @@ unsigned LibRaw::pana_data(int nb, unsigned *bytes) int byte; if (!nb && !bytes) - return vpos = 0; + { + memset(buf, 0, sizeof(buf)); + return vpos = 0; + } + if (load_flags > 0x4000) + throw LIBRAW_EXCEPTION_IO_BADFILE; if (!vpos) { - fread(buf + load_flags, 1, 0x4000 - load_flags, ifp); + if(load_flags < 0x4000) + fread(buf + load_flags, 1, 0x4000 - load_flags, ifp); fread(buf, 1, load_flags, ifp); } @@ -1409,7 +1415,7 @@ void LibRaw::sony_load_raw() void LibRaw::sony_arw_load_raw() { - std::vector huff_buffer(32770); + std::vector huff_buffer(32770,0); ushort* huff = &huff_buffer[0]; static const ushort tab[18] = {0xf11, 0xf10, 0xe0f, 0xd0e, 0xc0d, 0xb0c, 0xa0b, 0x90a, 0x809, 0x708, 0x607, 0x506, @@ -1441,7 +1447,7 @@ void LibRaw::sony_arw2_load_raw() ushort pix[16]; int row, col, val, max, min, imax, imin, sh, bit, i; - data = (uchar *)malloc(raw_width + 1); + data = (uchar *)calloc(raw_width + 1,1); try { for (row = 0; row < height; row++) diff --git a/rtengine/libraw/src/decoders/decoders_libraw.cpp b/rtengine/libraw/src/decoders/decoders_libraw.cpp index bc62a6c2d7..952e41a63d 100644 --- a/rtengine/libraw/src/decoders/decoders_libraw.cpp +++ b/rtengine/libraw/src/decoders/decoders_libraw.cpp @@ -54,11 +54,20 @@ void LibRaw::sony_arq_load_raw() void LibRaw::pentax_4shot_load_raw() { - ushort *plane = (ushort *)malloc(imgdata.sizes.raw_width * - imgdata.sizes.raw_height * sizeof(ushort)); +#ifdef LIBRAW_CALLOC_RAWSTORE + ushort *plane = (ushort *)calloc(size_t(imgdata.sizes.raw_width) * + size_t(imgdata.sizes.raw_height), sizeof(ushort)); +#else + ushort *plane = (ushort *)malloc(size_t(imgdata.sizes.raw_width) * + size_t(imgdata.sizes.raw_height) * sizeof(ushort)); +#endif int alloc_sz = imgdata.sizes.raw_width * (imgdata.sizes.raw_height + 16) * 4 * sizeof(ushort); +#ifdef LIBRAW_CALLOC_RAWSTORE + ushort(*result)[4] = (ushort(*)[4])calloc(alloc_sz,1); +#else ushort(*result)[4] = (ushort(*)[4])malloc(alloc_sz); +#endif struct movement_t { int row, col; @@ -197,7 +206,7 @@ void LibRaw::nikon_14bit_load_raw() (unsigned)(ceilf((float)(S.raw_width * 7 / 4) / 16.0)) * 16; // 14512; // S.raw_width * 7 / 4; const unsigned pitch = S.raw_pitch ? S.raw_pitch / 2 : S.raw_width; - unsigned char *buf = (unsigned char *)malloc(linelen); + unsigned char *buf = (unsigned char *)calloc(linelen,1); for (int row = 0; row < S.raw_height; row++) { unsigned bytesread = @@ -216,7 +225,7 @@ void LibRaw::fuji_14bit_load_raw() { const unsigned linelen = S.raw_width * 7 / 4; const unsigned pitch = S.raw_pitch ? S.raw_pitch / 2 : S.raw_width; - unsigned char *buf = (unsigned char *)malloc(linelen); + unsigned char *buf = (unsigned char *)calloc(linelen,1); for (int row = 0; row < S.raw_height; row++) { @@ -247,7 +256,7 @@ void LibRaw::nikon_load_padded_packed_raw() // 12 bit per pixel, padded to 16 libraw_internal_data.unpacker_data.load_flags > 64000) return; unsigned char *buf = - (unsigned char *)malloc(libraw_internal_data.unpacker_data.load_flags); + (unsigned char *)calloc(libraw_internal_data.unpacker_data.load_flags,1); for (int row = 0; row < S.raw_height; row++) { checkCancel(); @@ -478,7 +487,7 @@ void LibRaw::panasonicC7_load_raw() const int rowstep = 16; int pixperblock = libraw_internal_data.unpacker_data.pana_bpp == 14 ? 9 : 10; int rowbytes = imgdata.sizes.raw_width / pixperblock * 16; - unsigned char *iobuf = (unsigned char *)malloc(rowbytes * rowstep); + unsigned char *iobuf = (unsigned char *)calloc(rowbytes * rowstep,1); for (int row = 0; row < imgdata.sizes.raw_height - rowstep + 1; row += rowstep) { @@ -540,7 +549,7 @@ void LibRaw::unpacked_load_raw_fuji_f700s20() libraw_internal_data.internal_data.input->seek(-row_size, SEEK_CUR); base_offset = row_size; // in bytes } - unsigned char *buffer = (unsigned char *)malloc(row_size * 2); + unsigned char *buffer = (unsigned char *)calloc(row_size,2); for (int row = 0; row < imgdata.sizes.raw_height; row++) { read_shorts((ushort *)buffer, imgdata.sizes.raw_width * 2); @@ -554,7 +563,7 @@ void LibRaw::nikon_load_sraw() { // We're already seeked to data! unsigned char *rd = - (unsigned char *)malloc(3 * (imgdata.sizes.raw_width + 2)); + (unsigned char *)calloc(3 * (imgdata.sizes.raw_width + 2),1); if (!rd) throw LIBRAW_EXCEPTION_ALLOC; try diff --git a/rtengine/libraw/src/decoders/decoders_libraw_dcrdefs.cpp b/rtengine/libraw/src/decoders/decoders_libraw_dcrdefs.cpp index 9ce9dd467f..701e3da497 100644 --- a/rtengine/libraw/src/decoders/decoders_libraw_dcrdefs.cpp +++ b/rtengine/libraw/src/decoders/decoders_libraw_dcrdefs.cpp @@ -87,11 +87,11 @@ void LibRaw::sony_ljpeg_load_raw() break; try { - for (row = jrow = 0; jrow < (unsigned)jh.high; jrow++, row += 2) + for (row = jrow = 0; jrow < (unsigned)jh.high && trow+row < raw_height-1; jrow++, row += 2) { checkCancel(); ushort(*rowp)[4] = (ushort(*)[4])ljpeg_row(jrow, &jh); - for (col = jcol = 0; jcol < (unsigned)jh.wide; jcol++, col += 2) + for (col = jcol = 0; jcol < (unsigned)jh.wide && tcol+col < raw_width-1; jcol++, col += 2) { RAW(trow + row, tcol + col) = rowp[jcol][0]; RAW(trow + row, tcol + col + 1) = rowp[jcol][1]; @@ -129,7 +129,7 @@ void LibRaw::nikon_coolscan_load_raw() int bypp = tiff_bps <= 8 ? 1 : 2; int bufsize = width * clrs * bypp; - unsigned char *buf = (unsigned char *)malloc(bufsize); + unsigned char *buf = (unsigned char *)calloc(bufsize,1); unsigned short *ubuf = (unsigned short *)buf; if (tiff_bps <= 8) @@ -228,7 +228,7 @@ void LibRaw::android_tight_load_raw() int bwide, row, col, c; bwide = -(-5 * raw_width >> 5) << 3; - data = (uchar *)malloc(bwide); + data = (uchar *)calloc(bwide,1); for (row = 0; row < raw_height; row++) { if (fread(data, 1, bwide, ifp) < bwide) @@ -246,7 +246,7 @@ void LibRaw::android_loose_load_raw() UINT64 bitbuf = 0; bwide = (raw_width + 5) / 6 << 3; - data = (uchar *)malloc(bwide); + data = (uchar *)calloc(bwide,1); for (row = 0; row < raw_height; row++) { if (fread(data, 1, bwide, ifp) < bwide) @@ -289,7 +289,7 @@ void LibRaw::rpi_load_raw8() dwide = raw_width; else dwide = raw_stride; - data = (uchar *)malloc(dwide * 2); + data = (uchar *)calloc(dwide, 2); for (row = 0; row < raw_height; row++) { if (fread(data + dwide, 1, dwide, ifp) < dwide) derror(); FORC(dwide) data[c] = data[dwide + (c ^ rev)]; @@ -320,7 +320,7 @@ void LibRaw::rpi_load_raw12() dwide = (raw_width * 3 + 1) / 2; else dwide = raw_stride; - data = (uchar *)malloc(dwide * 2); + data = (uchar *)calloc(dwide, 2); for (row = 0; row < raw_height; row++) { if (fread(data + dwide, 1, dwide, ifp) < dwide) derror(); FORC(dwide) data[c] = data[dwide + (c ^ rev)]; @@ -351,7 +351,7 @@ void LibRaw::rpi_load_raw14() dwide = ((raw_width * 7) + 3) >> 2; else dwide = raw_stride; - data = (uchar *)malloc(dwide * 2); + data = (uchar *)calloc(dwide, 2); for (row = 0; row < raw_height; row++) { if (fread(data + dwide, 1, dwide, ifp) < dwide) derror(); FORC(dwide) data[c] = data[dwide + (c ^ rev)]; @@ -386,7 +386,7 @@ void LibRaw::rpi_load_raw16() dwide = (raw_width * 2); else dwide = raw_stride; - data = (uchar *)malloc(dwide * 2); + data = (uchar *)calloc(dwide, 2); for (row = 0; row < raw_height; row++) { if (fread(data + dwide, 1, dwide, ifp) < dwide) derror(); FORC(dwide) data[c] = data[dwide + (c ^ rev)]; diff --git a/rtengine/libraw/src/decoders/dng.cpp b/rtengine/libraw/src/decoders/dng.cpp index 4245e52f99..83fcff3204 100644 --- a/rtengine/libraw/src/decoders/dng.cpp +++ b/rtengine/libraw/src/decoders/dng.cpp @@ -188,12 +188,11 @@ void LibRaw::lossy_dng_load_raw() if (!image) throw LIBRAW_EXCEPTION_IO_CORRUPT; struct jpeg_decompress_struct cinfo; - JSAMPARRAY buf; - JSAMPLE(*pixel)[3]; + unsigned sorder = order, ntags, opcode, deg, i, j, c; unsigned trow = 0, tcol = 0, row, col; INT64 save = data_offset - 4; - ushort cur[3][256]; + ushort cur[4][256]; double coeff[9], tot; if (meta_offset) @@ -212,7 +211,7 @@ void LibRaw::lossy_dng_load_raw() continue; } fseek(ifp, 20, SEEK_CUR); - if ((c = get4()) > 2) + if ((c = get4()) > 3) break; fseek(ifp, 12, SEEK_CUR); if ((deg = get4()) > 8) @@ -231,13 +230,15 @@ void LibRaw::lossy_dng_load_raw() else { gamma_curve(1 / 2.4, 12.92, 1, 255); - FORC3 memcpy(cur[c], curve, sizeof cur[0]); + FORC4 memcpy(cur[c], curve, sizeof cur[0]); } struct jpeg_error_mgr pub; cinfo.err = jpeg_std_error(&pub); pub.error_exit = jpegErrorExit_d; + std::vector buf; + jpeg_create_decompress(&cinfo); while (trow < raw_height) @@ -252,19 +253,24 @@ void LibRaw::lossy_dng_load_raw() } jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); - buf = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, - cinfo.output_width * 3, 1); + if (cinfo.output_components != colors) + throw LIBRAW_EXCEPTION_DECODE_JPEG; + + if (buf.size() < cinfo.output_width * cinfo.output_components) + buf = std::vector(cinfo.output_width * cinfo.output_components,0); + try { + JSAMPLE *buffer_array[1]; + buffer_array[0] = buf.data(); while (cinfo.output_scanline < cinfo.output_height && (row = trow + cinfo.output_scanline) < height) { checkCancel(); - jpeg_read_scanlines(&cinfo, buf, 1); - pixel = (JSAMPLE(*)[3])buf[0]; + jpeg_read_scanlines(&cinfo, buffer_array, 1); for (col = 0; col < cinfo.output_width && tcol + col < width; col++) { - FORC3 image[row * width + tcol + col][c] = cur[c][pixel[col][c]]; + FORC(colors) image[row * width + tcol + col][c] = cur[c][buf[col*colors+c]]; } } } diff --git a/rtengine/libraw/src/decoders/fp_dng.cpp b/rtengine/libraw/src/decoders/fp_dng.cpp index f54a79ef33..9df3146467 100644 --- a/rtengine/libraw/src/decoders/fp_dng.cpp +++ b/rtengine/libraw/src/decoders/fp_dng.cpp @@ -291,8 +291,8 @@ void tile_stripe_data_t::init(tiff_ifd_t *ifd, const libraw_image_sizes_t& sizes if (tileCnt < 1 || tileCnt > 1000000) throw LIBRAW_EXCEPTION_DECODE_RAW; - tOffsets = std::vector(tileCnt); - tBytes = std::vector (tileCnt); + tOffsets = std::vector(tileCnt,0); + tBytes = std::vector (tileCnt,0); if (tiled) for (int t = 0; t < tileCnt; ++t) @@ -377,8 +377,8 @@ void LibRaw::deflate_dng_load_raw() if(INT64(tiles.maxBytesInTile) > INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024) ) throw LIBRAW_EXCEPTION_TOOBIG; - std::vector cBuffer(tiles.maxBytesInTile); - std::vector uBuffer(tileBytes + tileRowBytes); // extra row for decoding + std::vector cBuffer(tiles.maxBytesInTile,0); + std::vector uBuffer(tileBytes + tileRowBytes,0); // extra row for decoding for (size_t y = 0, t = 0; y < imgdata.sizes.raw_height; y += tiles.tileHeight) { @@ -623,7 +623,7 @@ void LibRaw::uncompressed_fp_dng_load_raw() bool difford = (libraw_internal_data.unpacker_data.order == 0x4949) == (ntohs(0x1234) == 0x1234); float max = 0.f; - std::vector rowbuf(tiles.tileWidth *sizeof(float) * ifd->samples); // line buffer for last tile in tile row + std::vector rowbuf(tiles.tileWidth *sizeof(float) * ifd->samples,0); // line buffer for last tile in tile row for (size_t y = 0, t = 0; y < imgdata.sizes.raw_height; y += tiles.tileHeight) { diff --git a/rtengine/libraw/src/decoders/kodak_decoders.cpp b/rtengine/libraw/src/decoders/kodak_decoders.cpp index 0706e4911a..679f25738c 100644 --- a/rtengine/libraw/src/decoders/kodak_decoders.cpp +++ b/rtengine/libraw/src/decoders/kodak_decoders.cpp @@ -55,7 +55,7 @@ void LibRaw::kodak_radc_load_raw() 3, -49, 3, -9, 3, 9, 4, 49, 5, -79, 5, 79, 2, -1, 2, 13, 2, 26, 3, 39, 4, -16, 5, 55, 6, -37, 6, 76, 2, -26, 2, -13, 2, 1, 3, -39, 4, 16, 5, -55, 6, -76, 6, 37}; - std::vector huff_buffer(19 * 256); + std::vector huff_buffer(19 * 256,0); ushort* huff = &huff_buffer[0]; int row, col, tree, nreps, rep, step, i, c, s, r, x, y, val; short last[3] = {16, 16, 16}, mul[3], buf[3][3][386]; @@ -181,8 +181,8 @@ void LibRaw::kodak_jpeg_load_raw() INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024)) throw LIBRAW_EXCEPTION_TOOBIG; - unsigned char *jpg_buf = (unsigned char *)malloc(data_size); - std::vector pixel_buf(width * 3); + unsigned char *jpg_buf = (unsigned char *)calloc(data_size,1); + std::vector pixel_buf(width * 3, 0); jpeg_create_decompress(&cinfo); fread(jpg_buf, data_size, 1, ifp); diff --git a/rtengine/libraw/src/decoders/unpack.cpp b/rtengine/libraw/src/decoders/unpack.cpp index c4f3359e74..3932372dd8 100644 --- a/rtengine/libraw/src/decoders/unpack.cpp +++ b/rtengine/libraw/src/decoders/unpack.cpp @@ -53,8 +53,13 @@ int LibRaw::unpack(void) INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024)) throw LIBRAW_EXCEPTION_TOOBIG; +#ifdef LIBRAW_CALLOC_RAWSTORE + libraw_internal_data.internal_data.meta_data = + (char *)calloc(libraw_internal_data.unpacker_data.meta_length,1); +#else libraw_internal_data.internal_data.meta_data = (char *)malloc(libraw_internal_data.unpacker_data.meta_length); +#endif } libraw_decoder_info_t decoder_info; @@ -325,8 +330,13 @@ int LibRaw::unpack(void) + +INT64(libraw_internal_data.unpacker_data.meta_length) > INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024)) throw LIBRAW_EXCEPTION_TOOBIG; +#ifdef LIBRAW_CALLOC_RAWSTORE + imgdata.rawdata.raw_alloc = calloc( + rwidth * (rheight + 8), sizeof(imgdata.rawdata.raw_image[0])); +#else imgdata.rawdata.raw_alloc = malloc( rwidth * (rheight + 8) * sizeof(imgdata.rawdata.raw_image[0])); +#endif imgdata.rawdata.raw_image = (ushort *)imgdata.rawdata.raw_alloc; if (!S.raw_pitch) S.raw_pitch = S.raw_width * 2; // Bayer case, not set before @@ -354,8 +364,13 @@ int LibRaw::unpack(void) INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024)) throw LIBRAW_EXCEPTION_TOOBIG; +#ifdef LIBRAW_CALLOC_RAWSTORE + imgdata.rawdata.raw_alloc = calloc( + rwidth * (rheight + 8), sizeof(imgdata.rawdata.raw_image[0]) * 3); +#else imgdata.rawdata.raw_alloc = malloc( rwidth * (rheight + 8) * sizeof(imgdata.rawdata.raw_image[0]) * 3); +#endif imgdata.rawdata.color3_image = (ushort(*)[3])imgdata.rawdata.raw_alloc; if (!S.raw_pitch) S.raw_pitch = S.raw_width * 6; diff --git a/rtengine/libraw/src/decoders/unpack_thumb.cpp b/rtengine/libraw/src/decoders/unpack_thumb.cpp index 1082b9326a..b0cfd62f28 100644 --- a/rtengine/libraw/src/decoders/unpack_thumb.cpp +++ b/rtengine/libraw/src/decoders/unpack_thumb.cpp @@ -155,7 +155,11 @@ int LibRaw::unpack_thumb(void) THUMB_SIZE_CHECKTNZ(T.tlength); if (T.thumb) free(T.thumb); +#ifdef LIBRAW_CALLOC_RAWSTORE + T.thumb = (char *)calloc(T.tlength,1); +#else T.thumb = (char *)malloc(T.tlength); +#endif if(!T.thumb) return LIBRAW_NO_THUMBNAIL; ID.input->read(T.thumb, 1, T.tlength); @@ -335,7 +339,11 @@ int LibRaw::unpack_thumb(void) THUMB_SIZE_CHECKTNZ(T.tlength); if (T.thumb) free(T.thumb); +#ifdef LIBRAW_CALLOC_RAWSTORE + T.thumb = (char *)calloc(T.tlength,1); +#else T.thumb = (char *)malloc(T.tlength); +#endif if (!T.thumb) return LIBRAW_NO_THUMBNAIL; @@ -370,8 +378,11 @@ int LibRaw::unpack_thumb(void) free(T.thumb); THUMB_SIZE_CHECKTNZ(T.tlength); - +#ifdef LIBRAW_CALLOC_RAWSTORE + T.thumb = (char *)calloc(T.tlength,1); +#else T.thumb = (char *)malloc(T.tlength); +#endif if (!T.thumb) return LIBRAW_NO_THUMBNAIL; if (!T.tcolors) @@ -413,7 +424,13 @@ int LibRaw::unpack_thumb(void) } else { +#ifdef LIBRAW_CALLOC_RAWSTORE + T.thumb = (char *)calloc(o_length,1); +#else T.thumb = (char *)malloc(o_length); +#endif + if (!T.thumb) + throw LIBRAW_EXCEPTION_ALLOC; if (!T.thumb) { free(t_thumb); diff --git a/rtengine/libraw/src/integration/dngsdk_glue.cpp b/rtengine/libraw/src/integration/dngsdk_glue.cpp index 0d0a9e8e38..fea957339a 100644 --- a/rtengine/libraw/src/integration/dngsdk_glue.cpp +++ b/rtengine/libraw/src/integration/dngsdk_glue.cpp @@ -118,7 +118,9 @@ int LibRaw::valid_for_dngsdk() if (libraw_internal_data.unpacker_data.tiff_compress == 34892 && libraw_internal_data.unpacker_data.tiff_bps == 8 - && (libraw_internal_data.unpacker_data.tiff_samples == 3 || libraw_internal_data.unpacker_data.tiff_samples == 1) + && (libraw_internal_data.unpacker_data.tiff_samples == 3 + || libraw_internal_data.unpacker_data.tiff_samples == 1 + || libraw_internal_data.unpacker_data.tiff_samples == 4 ) && load_raw == &LibRaw::lossy_dng_load_raw ) { @@ -217,7 +219,7 @@ int LibRaw::try_dngsdk() libraw_internal_data.unpacker_data.tiff_compress != 9 && #endif ( - ((libraw_internal_data.unpacker_data.tiff_compress == 34892 + ((libraw_internal_data.unpacker_data.tiff_compress == 34892 && libraw_internal_data.unpacker_data.tiff_bps == 8 && libraw_internal_data.unpacker_data.tiff_samples == 3 && load_raw == &LibRaw::lossy_dng_load_raw) // JPEG DNG or JPEG DNG RAW Preview diff --git a/rtengine/libraw/src/metadata/adobepano.cpp b/rtengine/libraw/src/metadata/adobepano.cpp index ee1d2c8a13..3bca3ebb7a 100644 --- a/rtengine/libraw/src/metadata/adobepano.cpp +++ b/rtengine/libraw/src/metadata/adobepano.cpp @@ -37,7 +37,7 @@ void LibRaw::parseAdobePanoMakernote() PrivateMknLength = get4(); if ((PrivateMknLength > 4) && (PrivateMknLength < 10240000) && - (PrivateMknBuf = (uchar *)malloc(PrivateMknLength + 1024))) + (PrivateMknBuf = (uchar *)calloc(PrivateMknLength + 1024,1))) { // 1024b for safety fread(PrivateMknBuf, PrivateMknLength, 1, ifp); PrivateOrder = sget2(PrivateMknBuf); diff --git a/rtengine/libraw/src/metadata/canon.cpp b/rtengine/libraw/src/metadata/canon.cpp index eab35e0e88..60d0db97ae 100644 --- a/rtengine/libraw/src/metadata/canon.cpp +++ b/rtengine/libraw/src/metadata/canon.cpp @@ -845,7 +845,7 @@ void LibRaw::parseCanonMakernotes(unsigned tag, unsigned /*type*/, unsigned len, imCommon.afdata[imCommon.afcount].AFInfoData_tag = tag; imCommon.afdata[imCommon.afcount].AFInfoData_order = order; imCommon.afdata[imCommon.afcount].AFInfoData_length = len; - imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)malloc(imCommon.afdata[imCommon.afcount].AFInfoData_length); + imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)calloc(imCommon.afdata[imCommon.afcount].AFInfoData_length,1); fread(imCommon.afdata[imCommon.afcount].AFInfoData, imCommon.afdata[imCommon.afcount].AFInfoData_length, 1, ifp); imCommon.afcount = 1; } diff --git a/rtengine/libraw/src/metadata/cr3_parser.cpp b/rtengine/libraw/src/metadata/cr3_parser.cpp index 4b0d1d1955..63047ca2e2 100644 --- a/rtengine/libraw/src/metadata/cr3_parser.cpp +++ b/rtengine/libraw/src/metadata/cr3_parser.cpp @@ -537,9 +537,9 @@ int LibRaw::parseCR3(INT64 oAtomList, fread(UIID, 1, lHdr, ifp); if (!memcmp(UIID, UUID_XMP, 16) && szAtom > 24LL && szAtom < 1024000LL) { - xmpdata = (char *)malloc(xmplen = unsigned(szAtom - 23)); - fread(xmpdata, szAtom - 24, 1, ifp); - xmpdata[szAtom - 24] = 0; + xmpdata = (char *)calloc(xmplen = unsigned(szAtom - 23),1); + unsigned br = fread(xmpdata,1, szAtom - 24, ifp); + xmpdata[br] = 0; } else if (!memcmp(UIID, UIID_CanonPreview, 16) && szAtom > 48LL && szAtom < 100LL * 1024000LL) { @@ -775,7 +775,7 @@ int LibRaw::parseCR3(INT64 oAtomList, goto fin; } - current_track.stsc_data = (crx_sample_to_chunk_t*) malloc(entries * sizeof(crx_sample_to_chunk_t)); + current_track.stsc_data = (crx_sample_to_chunk_t*) calloc(entries * sizeof(crx_sample_to_chunk_t),1); if(!current_track.stsc_data) { err = -9; @@ -812,7 +812,7 @@ int LibRaw::parseCR3(INT64 oAtomList, err = -10; goto fin; } - current_track.sample_sizes = (int32_t*)malloc(entries * sizeof(int32_t)); + current_track.sample_sizes = (int32_t*)calloc(entries * sizeof(int32_t),1); if (!current_track.sample_sizes) { err = -10; @@ -836,7 +836,7 @@ int LibRaw::parseCR3(INT64 oAtomList, err = -11; goto fin; } - current_track.chunk_offsets = (INT64*)malloc(entries * sizeof(int64_t)); + current_track.chunk_offsets = (INT64*)calloc(entries * sizeof(int64_t),1); if(!current_track.chunk_offsets) { err = -11; diff --git a/rtengine/libraw/src/metadata/fuji.cpp b/rtengine/libraw/src/metadata/fuji.cpp index 30e64c2ea8..362f188a43 100644 --- a/rtengine/libraw/src/metadata/fuji.cpp +++ b/rtengine/libraw/src/metadata/fuji.cpp @@ -800,7 +800,7 @@ void LibRaw::parseAdobeRAFMakernote() wb[2] = PrivateMknBuf.sget4(posWB) << 1; posWB += 4; - if (tWB && (iCCT < 255)) + if (tWB && (iCCT < 64)) { icWBCCTC[iCCT][0] = tWB; FORC4 icWBCCTC[iCCT][c + 1] = wb[c]; @@ -1089,9 +1089,9 @@ void LibRaw::parse_fuji_thumbnail(int offset) if ((fread(buf, 1, xmpsz, ifp) == xmpsz) && !memcmp(buf, xmpmarker, xmpsz)) // got it { xmplen = len - xmpsz - 2; - xmpdata = (char*) malloc(xmplen+1); - fread(xmpdata, 1, xmplen, ifp); - xmpdata[xmplen] = 0; + xmpdata = (char*) calloc(xmplen+1,1); + unsigned br = fread(xmpdata, 1, xmplen, ifp); + xmpdata[br] = 0; break; } } @@ -1309,7 +1309,7 @@ void LibRaw::parse_fuji(int offset) wb[1] = get4(); wb[3] = get4(); wb[2] = get4() << 1; - if (tWB && (iCCT < 255)) + if (tWB && (iCCT < 64)) { icWBCCTC[iCCT][0] = tWB; FORC4 icWBCCTC[iCCT][c + 1] = wb[c]; diff --git a/rtengine/libraw/src/metadata/identify_tools.cpp b/rtengine/libraw/src/metadata/identify_tools.cpp index caf1cddf0e..7568cfda5d 100644 --- a/rtengine/libraw/src/metadata/identify_tools.cpp +++ b/rtengine/libraw/src/metadata/identify_tools.cpp @@ -94,7 +94,7 @@ void LibRaw::remove_trailing_spaces(char *string, size_t len) if (len < 3) return; // also not needed len = strnlen(string, len - 1); - for (size_t i = len - 1; i >= 0; i--) + for (int i = int(len) - 1; i >= 0; i--) { if (isspace((unsigned char)string[i])) string[i] = 0; diff --git a/rtengine/libraw/src/metadata/makernotes.cpp b/rtengine/libraw/src/metadata/makernotes.cpp index 20b90397f9..40022b5c76 100644 --- a/rtengine/libraw/src/metadata/makernotes.cpp +++ b/rtengine/libraw/src/metadata/makernotes.cpp @@ -208,12 +208,12 @@ void LibRaw::parse_makernote_0xc634(int base, int uptag, unsigned dng_writer) { // camera info if (!tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG)) { - CanonCameraInfo = (uchar *)malloc(MAX(16, len)); + CanonCameraInfo = (uchar *)calloc(MAX(16, len),1); fread(CanonCameraInfo, len, 1, ifp); } else { - CanonCameraInfo = (uchar *)malloc(MAX(16, len * 4)); + CanonCameraInfo = (uchar *)calloc(MAX(16, len * 4),1); fread(CanonCameraInfo, len, 4, ifp); } lenCanonCameraInfo = len; @@ -561,12 +561,12 @@ void LibRaw::parse_makernote(int base, int uptag) { if (!tagtypeIs(LIBRAW_EXIFTAG_TYPE_LONG)) { - CanonCameraInfo = (uchar *)malloc(MAX(16, len)); + CanonCameraInfo = (uchar *)calloc(MAX(16, len),1); fread(CanonCameraInfo, len, 1, ifp); } else { - CanonCameraInfo = (uchar *)malloc(MAX(16, len * 4)); + CanonCameraInfo = (uchar *)calloc(MAX(16, len * 4),1); fread(CanonCameraInfo, len, 4, ifp); } lenCanonCameraInfo = len; diff --git a/rtengine/libraw/src/metadata/nikon.cpp b/rtengine/libraw/src/metadata/nikon.cpp index f0fb655d9c..f98f0ce424 100644 --- a/rtengine/libraw/src/metadata/nikon.cpp +++ b/rtengine/libraw/src/metadata/nikon.cpp @@ -605,7 +605,7 @@ uchar *cj_block, *ck_block; imCommon.afdata[imCommon.afcount].AFInfoData_tag = tag; imCommon.afdata[imCommon.afcount].AFInfoData_order = order; imCommon.afdata[imCommon.afcount].AFInfoData_length = len; - imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)malloc(imCommon.afdata[imCommon.afcount].AFInfoData_length); + imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)calloc(imCommon.afdata[imCommon.afcount].AFInfoData_length,1); fread(imCommon.afdata[imCommon.afcount].AFInfoData, imCommon.afdata[imCommon.afcount].AFInfoData_length, 1, ifp); imCommon.afcount = 1; } @@ -628,7 +628,7 @@ uchar *cj_block, *ck_block; else if ((tag == 0x0091) && (len > 4)) { ShotInfo_len = len; - ShotInfo_buf = (uchar *)malloc(ShotInfo_len); + ShotInfo_buf = (uchar *)calloc(ShotInfo_len,1); /* for dump: cj_block = (uchar *)malloc(ShotInfo_len); @@ -761,7 +761,7 @@ ck_block = (uchar *)malloc(ShotInfo_len); } if (LensData_len) { - LensData_buf = (uchar *)malloc(LensData_len); + LensData_buf = (uchar *)calloc(LensData_len,1); fread(LensData_buf, LensData_len, 1, ifp); } } @@ -921,7 +921,7 @@ free(ck_block); FORC4 ver = ver * 10 + (fgetc(ifp) - '0'); imCommon.afdata[imCommon.afcount].AFInfoData_version = ver; imCommon.afdata[imCommon.afcount].AFInfoData_length = len-4; - imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)malloc(imCommon.afdata[imCommon.afcount].AFInfoData_length); + imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)calloc(imCommon.afdata[imCommon.afcount].AFInfoData_length,1); fread(imCommon.afdata[imCommon.afcount].AFInfoData, imCommon.afdata[imCommon.afcount].AFInfoData_length, 1, ifp); imCommon.afcount = 1; } diff --git a/rtengine/libraw/src/metadata/normalize_model.cpp b/rtengine/libraw/src/metadata/normalize_model.cpp index da5ca2c029..993d28f584 100644 --- a/rtengine/libraw/src/metadata/normalize_model.cpp +++ b/rtengine/libraw/src/metadata/normalize_model.cpp @@ -1485,12 +1485,12 @@ printf("catchme\t%s\t%s\t%s\t9050%c\t%d\t%d\n", } void LibRaw::SetStandardIlluminants (unsigned makerIdx, const char* /*normModel*/) { - int i = -1; +// int i = -1; int c; if (!icWBC[LIBRAW_WBI_Ill_A][0] && !icWBC[LIBRAW_WBI_D65][0]) { if (makerIdx == LIBRAW_CAMERAMAKER_Olympus ) { - while (++i, icWBCCTC[i][0]) { + for (int i = 0; i < 64 && icWBCCTC[i][0]; i++) { if (icWBCCTC[i][0] == 3000) FORC4 icWBC[LIBRAW_WBI_Ill_A][c] = icWBCCTC[i][c+1]; else if (icWBCCTC[i][0] == 6600) diff --git a/rtengine/libraw/src/metadata/pentax.cpp b/rtengine/libraw/src/metadata/pentax.cpp index ae238d0aa8..4c57f79a02 100644 --- a/rtengine/libraw/src/metadata/pentax.cpp +++ b/rtengine/libraw/src/metadata/pentax.cpp @@ -136,7 +136,7 @@ void LibRaw::PentaxLensInfo(unsigned long long id, unsigned len) // tag 0x0207 { ushort iLensData = 0; uchar *table_buf; - table_buf = (uchar *)malloc(MAX(len, 128)); + table_buf = (uchar *)calloc(MAX(len, 128),1); fread(table_buf, len, 1, ifp); if ((id < PentaxID_K100D) || (((id == PentaxID_K100D) || @@ -419,7 +419,7 @@ void LibRaw::parsePentaxMakernotes(int /*base*/, unsigned tag, unsigned type, imCommon.afdata[imCommon.afcount].AFInfoData_tag = tag; imCommon.afdata[imCommon.afcount].AFInfoData_order = order; imCommon.afdata[imCommon.afcount].AFInfoData_length = len; - imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)malloc(imCommon.afdata[imCommon.afcount].AFInfoData_length); + imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)calloc(imCommon.afdata[imCommon.afcount].AFInfoData_length,1); fread(imCommon.afdata[imCommon.afcount].AFInfoData, imCommon.afdata[imCommon.afcount].AFInfoData_length, 1, ifp); if ((len < 25) && (len >= 11)) { @@ -466,7 +466,7 @@ void LibRaw::parsePentaxMakernotes(int /*base*/, unsigned tag, unsigned type, else if (tag == 0x0221) { int nWB = get2(); - if (nWB <= int(sizeof(icWBCCTC) / sizeof(icWBCCTC[0]))) + if (nWB > 0 && nWB <= int(sizeof(icWBCCTC) / sizeof(icWBCCTC[0]))) FORC(nWB) { icWBCCTC[c][0] = (unsigned)0xcfc6 - get2(); @@ -511,7 +511,7 @@ void LibRaw::parsePentaxMakernotes(int /*base*/, unsigned tag, unsigned type, imCommon.afdata[imCommon.afcount].AFInfoData_tag = tag; imCommon.afdata[imCommon.afcount].AFInfoData_order = order; imCommon.afdata[imCommon.afcount].AFInfoData_length = len; - imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)malloc(imCommon.afdata[imCommon.afcount].AFInfoData_length); + imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)calloc(imCommon.afdata[imCommon.afcount].AFInfoData_length,1); fread(imCommon.afdata[imCommon.afcount].AFInfoData, imCommon.afdata[imCommon.afcount].AFInfoData_length, 1, ifp); imCommon.afcount++; } diff --git a/rtengine/libraw/src/metadata/sony.cpp b/rtengine/libraw/src/metadata/sony.cpp index b63d0495de..5ddcfd1409 100644 --- a/rtengine/libraw/src/metadata/sony.cpp +++ b/rtengine/libraw/src/metadata/sony.cpp @@ -1204,7 +1204,7 @@ void LibRaw::parseSonyMakernotes( (tag == 0x0003)) && (len >= 196)) { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); lid = 0x01 << 2; @@ -1237,7 +1237,7 @@ void LibRaw::parseSonyMakernotes( else if ((tag == 0x0004) && // Minolta CameraSettings7D, big endian (len >= 227)) { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); lid = 0x0; @@ -1287,7 +1287,7 @@ void LibRaw::parseSonyMakernotes( ) { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); if (imCommon.afcount < LIBRAW_AFDATA_MAXCOUNT) { @@ -1395,7 +1395,7 @@ void LibRaw::parseSonyMakernotes( else if ((len == 19154) || // a200 a230 a290 a300 a330 a350 a380 a390 : FocusInfo (len == 19148)) // a700 a850 a900 : FocusInfo { - table_buf = (uchar *)malloc(0x0080); + table_buf = (uchar *)calloc(0x0080,1); fread(table_buf, 0x0080, 1, ifp); imgdata.shootinginfo.DriveMode = table_buf[14]; imgdata.shootinginfo.ExposureProgram = table_buf[63]; @@ -1413,7 +1413,7 @@ void LibRaw::parseSonyMakernotes( if ((a) && (c == 1)) { fseek(ifp, INT64(d) - 8LL, SEEK_CUR); - table_buf = (uchar *)malloc(256); + table_buf = (uchar *)calloc(256,1); fread(table_buf, 256, 1, ifp); imgdata.shootinginfo.DriveMode = table_buf[1]; imgdata.shootinginfo.ExposureProgram = table_buf[2]; @@ -1495,7 +1495,7 @@ void LibRaw::parseSonyMakernotes( (tag == 0xb0280114)) && (len < 256000)) { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); switch (len) { @@ -1707,7 +1707,7 @@ void LibRaw::parseSonyMakernotes( } else if ((tag == 0x3000) && (len < 256000)) { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); if (len >= 0x19) { @@ -1723,7 +1723,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x0116 && len < 256000) { - table_buf_0x0116 = (uchar *)malloc(len); + table_buf_0x0116 = (uchar *)calloc(len,1); table_buf_0x0116_len = len; fread(table_buf_0x0116, len, 1, ifp); if (ilm.CamID) @@ -1748,7 +1748,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x2010 && len < 256000) { - table_buf_0x2010 = (uchar *)malloc(len); + table_buf_0x2010 = (uchar *)calloc(len,1); table_buf_0x2010_len = len; fread(table_buf_0x2010, len, 1, ifp); if (ilm.CamID) @@ -1803,7 +1803,7 @@ void LibRaw::parseSonyMakernotes( imCommon.afdata[imCommon.afcount].AFInfoData_tag = tag; imCommon.afdata[imCommon.afcount].AFInfoData_order = order; imCommon.afdata[imCommon.afcount].AFInfoData_length = len; - imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)malloc(imCommon.afdata[imCommon.afcount].AFInfoData_length); + imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)calloc(imCommon.afdata[imCommon.afcount].AFInfoData_length,1); fread(imCommon.afdata[imCommon.afcount].AFInfoData, imCommon.afdata[imCommon.afcount].AFInfoData_length, 1, ifp); imSony.nAFPointsUsed = short(MIN(imCommon.afdata[imCommon.afcount].AFInfoData_length, sizeof imSony.AFPointsUsed)); @@ -1827,7 +1827,7 @@ void LibRaw::parseSonyMakernotes( imCommon.afdata[imCommon.afcount].AFInfoData_tag = tag; imCommon.afdata[imCommon.afcount].AFInfoData_order = order; imCommon.afdata[imCommon.afcount].AFInfoData_length = len; - imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)malloc(imCommon.afdata[imCommon.afcount].AFInfoData_length); + imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)calloc(imCommon.afdata[imCommon.afcount].AFInfoData_length,1); fread(imCommon.afdata[imCommon.afcount].AFInfoData, imCommon.afdata[imCommon.afcount].AFInfoData_length, 1, ifp); imCommon.afcount++; } @@ -1859,7 +1859,7 @@ void LibRaw::parseSonyMakernotes( imCommon.afdata[imCommon.afcount].AFInfoData_tag = tag; imCommon.afdata[imCommon.afcount].AFInfoData_order = order; imCommon.afdata[imCommon.afcount].AFInfoData_length = len; - imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)malloc(imCommon.afdata[imCommon.afcount].AFInfoData_length); + imCommon.afdata[imCommon.afcount].AFInfoData = (uchar *)calloc(imCommon.afdata[imCommon.afcount].AFInfoData_length,1); fread(imCommon.afdata[imCommon.afcount].AFInfoData, imCommon.afdata[imCommon.afcount].AFInfoData_length, 1, ifp); imCommon.afcount++; } @@ -1884,7 +1884,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x9050 && len < 256000) // little endian { - table_buf_0x9050 = (uchar *)malloc(len); + table_buf_0x9050 = (uchar *)calloc(len,1); table_buf_0x9050_len = len; fread(table_buf_0x9050, len, 1, ifp); @@ -1898,7 +1898,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x9400 && len < 256000) { - table_buf_0x9400 = (uchar *)malloc(len); + table_buf_0x9400 = (uchar *)calloc(len,1); table_buf_0x9400_len = len; fread(table_buf_0x9400, len, 1, ifp); if (ilm.CamID) @@ -1910,7 +1910,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x9402 && len < 256000) { - table_buf_0x9402 = (uchar *)malloc(len); + table_buf_0x9402 = (uchar *)calloc(len,1); table_buf_0x9402_len = len; fread(table_buf_0x9402, len, 1, ifp); if (ilm.CamID) @@ -1922,7 +1922,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x9403 && len < 256000) { - table_buf_0x9403 = (uchar *)malloc(len); + table_buf_0x9403 = (uchar *)calloc(len,1); table_buf_0x9403_len = len; fread(table_buf_0x9403, len, 1, ifp); if (ilm.CamID) @@ -1934,7 +1934,7 @@ void LibRaw::parseSonyMakernotes( } else if ((tag == 0x9405) && (len < 256000) && (len > 0x64)) { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); uc = table_buf[0x0]; if (imCommon.real_ISO < 0.1f) @@ -1952,7 +1952,7 @@ void LibRaw::parseSonyMakernotes( } else if ((tag == 0x9404) && (len < 256000) && (len > 0x21)) { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); uc = table_buf[0x00]; if (((uc == 0x70) || @@ -1972,7 +1972,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x9406 && len < 256000) { - table_buf_0x9406 = (uchar *)malloc(len); + table_buf_0x9406 = (uchar *)calloc(len,1); table_buf_0x9406_len = len; fread(table_buf_0x9406, len, 1, ifp); if (ilm.CamID) @@ -1984,7 +1984,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x940c && len < 256000) { - table_buf_0x940c = (uchar *)malloc(len); + table_buf_0x940c = (uchar *)calloc(len,1); table_buf_0x940c_len = len; fread(table_buf_0x940c, len, 1, ifp); if (ilm.CamID) @@ -1996,7 +1996,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0x940e && len < 256000) { - table_buf_0x940e = (uchar *)malloc(len); + table_buf_0x940e = (uchar *)calloc(len,1); table_buf_0x940e_len = len; fread(table_buf_0x940e, len, 1, ifp); if (ilm.CamID) @@ -2007,7 +2007,7 @@ void LibRaw::parseSonyMakernotes( } } else if ((tag == 0x9416) && (len < 256000) && (len > 0x0076)) { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); if (imCommon.real_ISO < 0.1f) { s[0] = SonySubstitution[table_buf[0x04]]; @@ -2072,7 +2072,7 @@ void LibRaw::parseSonyMakernotes( } else if (tag == 0xb02a && len < 256000) // Sony LensSpec { - table_buf = (uchar *)malloc(len); + table_buf = (uchar *)calloc(len,1); fread(table_buf, len, 1, ifp); if ((!dng_writer) || (saneSonyCameraInfo(table_buf[1], table_buf[2], table_buf[3], @@ -2207,10 +2207,12 @@ void LibRaw::parseSonySR2(uchar *_cbuf_SR2, unsigned SR2SubIFDOffset, if ((tag_id >= 0x7480) && (tag_id <= 0x7486)) { i = tag_id - 0x7480; if (Sony_SR2_wb_list[i] > 255) { + if (WBCTC_count < 64) { icWBCCTC[WBCTC_count][0] = Sony_SR2_wb_list[i]; FORC3 icWBCCTC[WBCTC_count][c + 1] = cbuf_SR2.sget2(tag_dataoffset + tag_dataunitlen * c); icWBCCTC[WBCTC_count][4] = icWBCCTC[WBCTC_count][2]; - WBCTC_count++; + } + WBCTC_count++; } else { FORC3 icWBC[Sony_SR2_wb_list[i]][c] = cbuf_SR2.sget2(tag_dataoffset + tag_dataunitlen * c); @@ -2220,6 +2222,7 @@ void LibRaw::parseSonySR2(uchar *_cbuf_SR2, unsigned SR2SubIFDOffset, else if ((tag_id >= 0x7820) && (tag_id <= 0x782d)) { i = tag_id - 0x7820; if (Sony_SR2_wb_list1[i] > 255) { + if (WBCTC_count < 64) { icWBCCTC[WBCTC_count][0] = Sony_SR2_wb_list1[i]; FORC3 icWBCCTC[WBCTC_count][c + 1] = cbuf_SR2.sget2(tag_dataoffset + tag_dataunitlen * c); icWBCCTC[WBCTC_count][4] = icWBCCTC[WBCTC_count][2]; @@ -2227,7 +2230,8 @@ void LibRaw::parseSonySR2(uchar *_cbuf_SR2, unsigned SR2SubIFDOffset, FORC3 icWBC[LIBRAW_WBI_StudioTungsten][c] = icWBCCTC[WBCTC_count][c + 1]; icWBC[LIBRAW_WBI_StudioTungsten][3] = icWBC[LIBRAW_WBI_StudioTungsten][1]; } - WBCTC_count++; + } + WBCTC_count++; } else { FORC3 icWBC[Sony_SR2_wb_list1[i]][c] = cbuf_SR2.sget2(tag_dataoffset + tag_dataunitlen * c); diff --git a/rtengine/libraw/src/metadata/tiff.cpp b/rtengine/libraw/src/metadata/tiff.cpp index 9c1650c1a6..9f9d8a3b6a 100644 --- a/rtengine/libraw/src/metadata/tiff.cpp +++ b/rtengine/libraw/src/metadata/tiff.cpp @@ -175,10 +175,12 @@ int LibRaw::parse_tiff_ifd(int base) { if ((j = get2()) < 0x100) { - icWBC[j][0] = get2(); - icWBC[j][2] = get2(); - icWBC[j][1] = icWBC[j][3] = - 0x100; + if (j >= 0) { + icWBC[j][0] = get2(); + icWBC[j][2] = get2(); + icWBC[j][1] = icWBC[j][3] = + 0x100; + } } else // light source out of EXIF numbers range get4(); @@ -492,12 +494,14 @@ int LibRaw::parse_tiff_ifd(int base) { if ((j = get2()) < 0x100) { + if(j >= 0) { icWBC[j][0] = get2(); icWBC[j][1] = icWBC[j][3] = get2(); icWBC[j][2] = get2(); if (c == 1 && i > 6 && cam_mul[0] <= 0.001f) for (int q = 0; q < 4; q++) cam_mul[q] = icWBC[j][q]; + } } else fseek(ifp, 6, SEEK_CUR); @@ -723,9 +727,9 @@ int LibRaw::parse_tiff_ifd(int base) tagtypeIs(LIBRAW_EXIFTOOLTAGTYPE_binary)) && (len > 1) && (len < 5100000)) { - xmpdata = (char *)malloc(xmplen = len + 1); - fread(xmpdata, len, 1, ifp); - xmpdata[len] = 0; + xmpdata = (char *)calloc(xmplen = len + 1,1); + unsigned br = fread(xmpdata,1, len, ifp); + xmpdata[br] = 0; } break; case 0x7000: @@ -1122,8 +1126,8 @@ int LibRaw::parse_tiff_ifd(int base) (libraw_internal_data.unpacker_data.lenRAFData < 10240000)) { INT64 f_save = ftell(ifp); - rafdata = (ushort *)malloc( - sizeof(ushort) * libraw_internal_data.unpacker_data.lenRAFData); + rafdata = (ushort *)calloc( + sizeof(ushort) * libraw_internal_data.unpacker_data.lenRAFData,1); fseek(ifp, libraw_internal_data.unpacker_data.posRAFData, SEEK_SET); fread(rafdata, sizeof(ushort), libraw_internal_data.unpacker_data.lenRAFData, ifp); @@ -1573,7 +1577,7 @@ int LibRaw::parse_tiff_ifd(int base) } if (SR2SubIFDLength && (SR2SubIFDLength < 10240000) && - (buf_SR2 = (unsigned *)malloc(SR2SubIFDLength + 1024))) + (buf_SR2 = (unsigned *)calloc(SR2SubIFDLength + 1024,1))) { // 1024b for safety fseek(ifp, SR2SubIFDOffset + base, SEEK_SET); fread(buf_SR2, SR2SubIFDLength, 1, ifp); @@ -1662,7 +1666,7 @@ int LibRaw::parse_tiff_ifd(int base) fseek(ifp, save, SEEK_SET); } if (sony_length && sony_length < 10240000 && - (buf = (unsigned *)malloc(sony_length))) + (buf = (unsigned *)calloc(sony_length, 1))) { fseek(ifp, sony_offset, SEEK_SET); fread(buf, sony_length, 1, ifp); diff --git a/rtengine/libraw/src/postprocessing/dcraw_process.cpp b/rtengine/libraw/src/postprocessing/dcraw_process.cpp index 8b3d77cf62..3a6f88d440 100644 --- a/rtengine/libraw/src/postprocessing/dcraw_process.cpp +++ b/rtengine/libraw/src/postprocessing/dcraw_process.cpp @@ -218,7 +218,7 @@ int LibRaw::dcraw_process(void) if (!libraw_internal_data.output_data.histogram) { libraw_internal_data.output_data.histogram = - (int(*)[LIBRAW_HISTOGRAM_SIZE])malloc( + (int(*)[LIBRAW_HISTOGRAM_SIZE])calloc(1, sizeof(*libraw_internal_data.output_data.histogram) * 4); } #ifndef NO_LCMS diff --git a/rtengine/libraw/src/utils/open.cpp b/rtengine/libraw/src/utils/open.cpp index a1c1917cff..258a8cf10c 100644 --- a/rtengine/libraw/src/utils/open.cpp +++ b/rtengine/libraw/src/utils/open.cpp @@ -1266,7 +1266,7 @@ int LibRaw::open_datastream(LibRaw_abstract_datastream *stream) INT64 profile_sz = MIN(INT64(C.profile_length), ID.input->size() - ID.profile_offset); if (profile_sz > 0LL && profile_sz < LIBRAW_MAX_PROFILE_SIZE_MB * 1024LL * 1024LL) { - C.profile = malloc(size_t(profile_sz)); + C.profile = calloc(size_t(profile_sz),1); ID.input->seek(ID.profile_offset, SEEK_SET); ID.input->read(C.profile, size_t(profile_sz), 1); } diff --git a/rtengine/libraw/src/utils/utils_libraw.cpp b/rtengine/libraw/src/utils/utils_libraw.cpp index dc81d0dd30..36dfc6003b 100644 --- a/rtengine/libraw/src/utils/utils_libraw.cpp +++ b/rtengine/libraw/src/utils/utils_libraw.cpp @@ -647,7 +647,7 @@ char** LibRaw::malloc_omp_buffers(int buffer_count, size_t buffer_size) for (int i = 0; i < buffer_count; i++) { - buffers[i] = (char*)malloc(buffer_size); + buffers[i] = (char*)calloc(buffer_size,1); } return buffers; } diff --git a/rtengine/libraw/src/write/apply_profile.cpp b/rtengine/libraw/src/write/apply_profile.cpp index 9c8a9fd986..d2a0fec135 100644 --- a/rtengine/libraw/src/write/apply_profile.cpp +++ b/rtengine/libraw/src/write/apply_profile.cpp @@ -48,7 +48,7 @@ void LibRaw::apply_profile(const char *input, const char *output) { fread(&size, 4, 1, fp); fseek(fp, 0, SEEK_SET); - oprof = (unsigned *)malloc(size = ntohl(size)); + oprof = (unsigned *)calloc(size = ntohl(size),1); fread(oprof, 1, size, fp); fclose(fp); if (!(hOutProfile = cmsOpenProfileFromMem(oprof, size)))