@@ -433,10 +433,11 @@ jpeg_read_comment(FIBITMAP *dib, const uint8_t *dataptr, unsigned int datalen) {
433433 uint8_t *profile = (uint8_t *)dataptr;
434434
435435 // read the comment
436- std::unique_ptr<void , decltype (&free)> safeValue (malloc ((length + 1 ) * sizeof (char )), &free);
437- if (!safeValue) return FALSE ;
438- auto *value = static_cast <char *>(safeValue.get ());
439- memcpy (value, profile, length);
436+ std::unique_ptr<char []> value (new (std::nothrow) char [length + 1 ]);
437+ if (!value) {
438+ return FALSE ;
439+ }
440+ memcpy (value.get (), profile, length);
440441 value[length] = ' \0 ' ;
441442
442443 bool bSuccess{};
@@ -449,7 +450,7 @@ jpeg_read_comment(FIBITMAP *dib, const uint8_t *dataptr, unsigned int datalen) {
449450 bSuccess = bSuccess && FreeImage_SetTagLength (tag.get (), count);
450451 bSuccess = bSuccess && FreeImage_SetTagCount (tag.get (), count);
451452 bSuccess = bSuccess && FreeImage_SetTagType (tag.get (), FIDT_ASCII);
452- bSuccess = bSuccess && FreeImage_SetTagValue (tag.get (), value);
453+ bSuccess = bSuccess && FreeImage_SetTagValue (tag.get (), value. get () );
453454
454455 // store the tag
455456 bSuccess = bSuccess && FreeImage_SetMetadata (FIMD_COMMENTS, dib, FreeImage_GetTagKey (tag.get ()), tag.get ());
@@ -506,7 +507,6 @@ jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr, unsigned *i
506507 jpeg_saved_marker_ptr marker;
507508 int num_markers = 0 ;
508509 int seq_no;
509- JOCTET *icc_data;
510510 unsigned total_length;
511511
512512 const int MAX_SEQ_NO = 255 ; // sufficient since marker numbers are bytes
@@ -563,13 +563,16 @@ jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr, unsigned *i
563563 total_length += data_length[seq_no];
564564 }
565565
566- if (total_length <= 0 )
567- return FALSE ; // found only empty markers ?
566+ if (total_length <= 0 ) {
567+ return FALSE ; // found only empty markers ?
568+ }
568569
569570 // allocate space for assembled data
570- icc_data = (JOCTET *) malloc (total_length * sizeof (JOCTET));
571- if (!icc_data)
572- return FALSE ; // out of memory
571+ std::unique_ptr<void , decltype (&free)> safeIcc (malloc (total_length * sizeof (JOCTET), &free);
572+ auto *icc_data = static_cast <JOCTET*>(safeIcc.get ());
573+ if (!icc_data) {
574+ return FALSE ; // out of memory
575+ }
573576
574577 // and fill it in
575578 for (marker = cinfo->marker_list ; marker; marker = marker->next ) {
@@ -589,6 +592,7 @@ jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr, unsigned *i
589592
590593 *icc_data_ptr = icc_data;
591594 *icc_data_len = total_length;
595+ safeIcc.release ();
592596
593597 return TRUE ;
594598}
@@ -786,9 +790,11 @@ jpeg_write_icc_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
786790 if (iccProfile->size && iccProfile->data ) {
787791 // ICC_HEADER_SIZE: ICC signature is 'ICC_PROFILE' + 2 bytes
788792
789- auto *profile = (uint8_t *)malloc ((iccProfile->size + ICC_HEADER_SIZE) * sizeof (uint8_t ));
790- if (!profile) return FALSE ;
791- memcpy (profile, icc_signature, 12 );
793+ std::unique_ptr<uint8_t []> profile (new (std::nothrow) uint8_t [iccProfile->size + ICC_HEADER_SIZE]);
794+ if (!profile) {
795+ return FALSE ;
796+ }
797+ memcpy (profile.get (), icc_signature, 12 );
792798
793799 for (long i = 0 ; i < (long )iccProfile->size ; i += MAX_DATA_BYTES_IN_MARKER) {
794800 unsigned length = std::min ((long )(iccProfile->size - i), MAX_DATA_BYTES_IN_MARKER);
@@ -797,12 +803,10 @@ jpeg_write_icc_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
797803 // number of markers
798804 profile[13 ] = (uint8_t ) (iccProfile->size / MAX_DATA_BYTES_IN_MARKER + 1 );
799805
800- memcpy (profile + ICC_HEADER_SIZE, (uint8_t *)iccProfile->data + i, length);
801- jpeg_write_marker (cinfo, ICC_MARKER, profile, (length + ICC_HEADER_SIZE));
806+ memcpy (profile. get () + ICC_HEADER_SIZE, (uint8_t *)iccProfile->data + i, length);
807+ jpeg_write_marker (cinfo, ICC_MARKER, profile. get () , (length + ICC_HEADER_SIZE));
802808 }
803809
804- free (profile);
805-
806810 return TRUE ;
807811 }
808812
@@ -829,8 +833,10 @@ jpeg_write_iptc_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
829833 for (long i = 0 ; i < (long )profile_size; i += 65517L ) {
830834 unsigned length = std::min ((long )profile_size - i, 65517L );
831835 unsigned roundup = length & 0x01 ; // needed for Photoshop
832- auto *iptc_profile = (uint8_t *)malloc (length + roundup + tag_length);
833- if (!iptc_profile) break ;
836+ std::unique_ptr<uint8_t []> iptc_profile (new (std::nothrow) uint8_t [length + roundup + tag_length]);
837+ if (!iptc_profile) {
838+ break ;
839+ }
834840 // Photoshop identification string
835841 memcpy (&iptc_profile[0 ], " Photoshop 3.0\x0 " , 14 );
836842 // 8BIM segment type
@@ -842,8 +848,7 @@ jpeg_write_iptc_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
842848 memcpy (&iptc_profile[tag_length], &profile[i], length);
843849 if (roundup)
844850 iptc_profile[length + tag_length] = 0 ;
845- jpeg_write_marker (cinfo, IPTC_MARKER, iptc_profile, length + roundup + tag_length);
846- free (iptc_profile);
851+ jpeg_write_marker (cinfo, IPTC_MARKER, iptc_profile.get (), length + roundup + tag_length);
847852 }
848853
849854 // release profile
@@ -877,19 +882,19 @@ jpeg_write_xmp_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
877882
878883 uint32_t tag_length = FreeImage_GetTagLength (tag_xmp);
879884
880- auto *profile = (uint8_t *)malloc ((tag_length + xmp_header_size) * sizeof (uint8_t ));
881- if (!profile) return FALSE ;
882- memcpy (profile, xmp_signature, xmp_header_size);
885+ std::unique_ptr<uint8_t []> profile (new (std::nothrow) uint8_t [tag_length + xmp_header_size]);
886+ if (!profile) {
887+ return FALSE ;
888+ }
889+ memcpy (profile.get (), xmp_signature, xmp_header_size);
883890
884891 for (uint32_t i = 0 ; i < tag_length; i += 65504L ) {
885892 unsigned length = std::min ((long )(tag_length - i), 65504L );
886893
887- memcpy (profile + xmp_header_size, tag_value + i, length);
888- jpeg_write_marker (cinfo, EXIF_MARKER, profile, (length + xmp_header_size));
894+ memcpy (profile. get () + xmp_header_size, tag_value + i, length);
895+ jpeg_write_marker (cinfo, EXIF_MARKER, profile. get () , (length + xmp_header_size));
889896 }
890897
891- free (profile);
892-
893898 return TRUE ;
894899 }
895900 }
@@ -921,18 +926,18 @@ jpeg_write_exif_profile_raw(j_compress_ptr cinfo, FIBITMAP *dib) {
921926 if (tag_value) {
922927 uint32_t tag_length = FreeImage_GetTagLength (tag_exif);
923928
924- auto *profile = (uint8_t *)malloc (tag_length * sizeof (uint8_t ));
925- if (!profile) return FALSE ;
929+ std::unique_ptr<uint8_t []> profile (new (std::nothrow) uint8_t [tag_length]);
930+ if (!profile) {
931+ return FALSE ;
932+ }
926933
927934 for (uint32_t i = 0 ; i < tag_length; i += 65504L ) {
928935 unsigned length = std::min ((long )(tag_length - i), 65504L );
929936
930- memcpy (profile, tag_value + i, length);
931- jpeg_write_marker (cinfo, EXIF_MARKER, profile, length);
937+ memcpy (profile. get () , tag_value + i, length);
938+ jpeg_write_marker (cinfo, EXIF_MARKER, profile. get () , length);
932939 }
933940
934- free (profile);
935-
936941 return TRUE ;
937942 }
938943 }
@@ -1576,8 +1581,9 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
15761581 if (color_type == FIC_RGB) {
15771582 // 24-bit RGB image : need to swap red and blue channels
15781583 unsigned pitch = FreeImage_GetPitch (dib);
1579- auto *target = (uint8_t *)malloc (pitch * sizeof (uint8_t ));
1580- if (!target) {
1584+ std::unique_ptr<uint8_t []> safeTarget (new (std::nothrow) uint8_t [pitch]);
1585+ auto *target{ safeTarget.get () };
1586+ if (target) {
15811587 throw FI_MSG_ERROR_MEMORY;
15821588 }
15831589
@@ -1595,11 +1601,11 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
15951601 // write the scanline
15961602 jpeg_write_scanlines (&cinfo, &target, 1 );
15971603 }
1598- free (target);
15991604 }
16001605 else if (color_type == FIC_CMYK) {
16011606 unsigned pitch = FreeImage_GetPitch (dib);
1602- auto *target = (uint8_t *)malloc (pitch * sizeof (uint8_t ));
1607+ std::unique_ptr<uint8_t []> safeTarget (new (std::nothrow) uint8_t [pitch]);
1608+ auto *target{ safeTarget.get () };
16031609 if (!target) {
16041610 throw FI_MSG_ERROR_MEMORY;
16051611 }
@@ -1622,7 +1628,6 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
16221628 // write the scanline
16231629 jpeg_write_scanlines (&cinfo, &target, 1 );
16241630 }
1625- free (target);
16261631 }
16271632 else if (color_type == FIC_MINISBLACK) {
16281633 // 8-bit standard greyscale images
@@ -1635,7 +1640,8 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
16351640 else if (color_type == FIC_PALETTE) {
16361641 // 8-bit palettized images are converted to 24-bit images
16371642 FIRGBA8 *palette = FreeImage_GetPalette (dib);
1638- auto *target = (uint8_t *)malloc (cinfo.image_width * 3 );
1643+ std::unique_ptr<uint8_t []> safeTarget (new (std::nothrow) uint8_t [cinfo.image_width * 3 ]);
1644+ auto *target{ safeTarget.get () };
16391645 if (!target) {
16401646 throw FI_MSG_ERROR_MEMORY;
16411647 }
@@ -1656,14 +1662,13 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
16561662
16571663 jpeg_write_scanlines (&cinfo, &target, 1 );
16581664 }
1659-
1660- free (target);
16611665 }
16621666 else if (color_type == FIC_MINISWHITE) {
16631667 // reverse 8-bit greyscale image, so reverse grey value on the fly
16641668 unsigned i;
16651669 uint8_t reverse[256 ];
1666- auto *target = (uint8_t *)malloc (cinfo.image_width );
1670+ std::unique_ptr<uint8_t []> safeTarget (new (std::nothrow) uint8_t [cinfo.image_width ]);
1671+ auto *target{ safeTarget.get () };
16671672 if (!target) {
16681673 throw FI_MSG_ERROR_MEMORY;
16691674 }
@@ -1679,8 +1684,6 @@ Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void
16791684 }
16801685 jpeg_write_scanlines (&cinfo, &target, 1 );
16811686 }
1682-
1683- free (target);
16841687 }
16851688
16861689 // Step 8: Finish compression
0 commit comments