print addons: remove invalid use of auto_ptr

GCC 13 warns that the use of auto_ptr on an array can lead to undefined
behavior, because auto_ptr will use `delete` instead of `delete[]`.

Replaced the use with `std::vector` instead. Since the `data()` member was only
introduced in C++11, the solution uses the [] operator instead to get the
address of the beginning of the array.

Change-Id: Ib457580eedb03338a454fb96b27602c3830c1634
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6644
Reviewed-by: Adrien Destugues <[email protected]>
Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
Niels Sascha Reedijk
2023-06-28 07:11:17 +00:00
committed by Adrien Destugues
parent 1f2e9ca260
commit fdc8b001a9
5 changed files with 100 additions and 112 deletions
@@ -6,7 +6,7 @@
#include "Lips3.h" #include "Lips3.h"
#include <memory> #include <vector>
#include <Alert.h> #include <Alert.h>
#include <Bitmap.h> #include <Bitmap.h>
@@ -162,13 +162,10 @@ LIPS3Driver::NextBand(BBitmap* bitmap, BPoint* offset)
int compressedSize; int compressedSize;
const uchar* buffer; const uchar* buffer;
uchar* in_buffer = new uchar[in_size]; std::vector<uchar> in_buffer(in_size);
uchar* out_buffer = new uchar[out_size]; std::vector<uchar> out_buffer(out_size);
auto_ptr<uchar> _in_buffer (in_buffer); uchar* ptr2 = &in_buffer[0];
auto_ptr<uchar> _out_buffer(out_buffer);
uchar* ptr2 = static_cast<uchar*>(in_buffer);
DBGMSG(("move\n")); DBGMSG(("move\n"));
@@ -181,12 +178,12 @@ LIPS3Driver::NextBand(BBitmap* bitmap, BPoint* offset)
y++; y++;
} }
compressedSize = compress3(out_buffer, in_buffer, in_size); compressedSize = compress3(&out_buffer[0], &in_buffer[0], in_size);
if (compressedSize < in_size) { if (compressedSize < in_size) {
compressionMethod = 9; compressionMethod = 9;
// compress3 // compress3
buffer = out_buffer; buffer = &out_buffer[0];
} else if (compressedSize > out_size) { } else if (compressedSize > out_size) {
BAlert* alert = new BAlert("memory overrun!!!", "warning", BAlert* alert = new BAlert("memory overrun!!!", "warning",
"OK"); "OK");
@@ -195,7 +192,7 @@ LIPS3Driver::NextBand(BBitmap* bitmap, BPoint* offset)
return false; return false;
} else { } else {
compressionMethod = 0; compressionMethod = 0;
buffer = in_buffer; buffer = &in_buffer[0];
compressedSize = in_size; compressedSize = in_size;
} }
@@ -6,7 +6,7 @@
#include "Lips4Cap.h" #include "Lips4Cap.h"
#include <memory> #include <vector>
#include <Alert.h> #include <Alert.h>
#include <Bitmap.h> #include <Bitmap.h>
@@ -172,13 +172,10 @@ LIPS4Driver::NextBand(BBitmap* bitmap, BPoint* offset)
int compressed_size; int compressed_size;
const uchar* buffer; const uchar* buffer;
uchar* in_buffer = new uchar[in_size]; std::vector<uchar> in_buffer(in_size);
uchar* out_buffer = new uchar[out_size]; std::vector<uchar> out_buffer(out_size);
auto_ptr<uchar> _in_buffer (in_buffer); uchar* ptr2 = &in_buffer[0];
auto_ptr<uchar> _out_buffer(out_buffer);
uchar* ptr2 = static_cast<uchar *>(in_buffer);
DBGMSG(("move\n")); DBGMSG(("move\n"));
@@ -193,11 +190,11 @@ LIPS4Driver::NextBand(BBitmap* bitmap, BPoint* offset)
DBGMSG(("PackBits\n")); DBGMSG(("PackBits\n"));
compressed_size = pack_bits(out_buffer, in_buffer, in_size); compressed_size = pack_bits(&out_buffer[0], &in_buffer[0], in_size);
if (compressed_size < in_size) { if (compressed_size < in_size) {
compression_method = 11; compression_method = 11;
buffer = out_buffer; buffer = &out_buffer[0];
} else if (compressed_size > out_size) { } else if (compressed_size > out_size) {
BAlert* alert = new BAlert("memory overrun!!!", "warning", "OK"); BAlert* alert = new BAlert("memory overrun!!!", "warning", "OK");
alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
@@ -205,7 +202,7 @@ LIPS4Driver::NextBand(BBitmap* bitmap, BPoint* offset)
return false; return false;
} else { } else {
compression_method = 0; compression_method = 0;
buffer = in_buffer; buffer = &in_buffer[0];
compressed_size = in_size; compressed_size = in_size;
} }
+7 -10
View File
@@ -7,7 +7,7 @@
#include "PCL5.h" #include "PCL5.h"
#include <memory> #include <vector>
#include <Alert.h> #include <Alert.h>
#include <Bitmap.h> #include <Bitmap.h>
@@ -148,11 +148,8 @@ PCL5Driver::NextBand(BBitmap* bitmap, BPoint* offset)
int compressedSize; int compressedSize;
const uchar* buffer; const uchar* buffer;
uchar* in_buffer = new uchar[in_size]; std::vector<uchar> in_buffer(in_size);
uchar* out_buffer = new uchar[out_size]; std::vector<uchar> out_buffer(out_size);
auto_ptr<uchar> _in_buffer (in_buffer);
auto_ptr<uchar> _out_buffer(out_buffer);
DBGMSG(("move\n")); DBGMSG(("move\n"));
@@ -171,17 +168,17 @@ PCL5Driver::NextBand(BBitmap* bitmap, BPoint* offset)
for (int plane = 0; plane < num_planes; plane ++) { for (int plane = 0; plane < num_planes; plane ++) {
fHalftone->Dither(in_buffer, ptr, x, y, width); fHalftone->Dither(&in_buffer[0], ptr, x, y, width);
compressedSize = pack_bits(out_buffer, in_buffer, in_size); compressedSize = pack_bits(&out_buffer[0], &in_buffer[0], in_size);
if (compressedSize + _BytesToEnterCompressionMethod(2) if (compressedSize + _BytesToEnterCompressionMethod(2)
< in_size + _BytesToEnterCompressionMethod(0)) { < in_size + _BytesToEnterCompressionMethod(0)) {
compressionMethod = 2; // back bits compressionMethod = 2; // back bits
buffer = out_buffer; buffer = &out_buffer[0];
} else { } else {
compressionMethod = 0; // uncompressed compressionMethod = 0; // uncompressed
buffer = in_buffer; buffer = &in_buffer[0];
compressedSize = in_size; compressedSize = in_size;
} }
+8 -11
View File
@@ -8,7 +8,7 @@
#include "PS.h" #include "PS.h"
#include <memory.h> #include <vector>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -266,14 +266,11 @@ PSDriver::NextBand(BBitmap* bitmap, BPoint* offset)
int compressed_size; int compressed_size;
const uchar* buffer; const uchar* buffer;
uchar* in_buffer = new uchar[in_size]; std::vector<uchar> in_buffer(in_size);
// gray values // gray values
uchar* out_buffer = new uchar[out_size]; std::vector<uchar> out_buffer(out_size);
// gray values in hexadecimal // gray values in hexadecimal
auto_ptr<uchar> _in_buffer(in_buffer);
auto_ptr<uchar> _out_buffer(out_buffer);
DBGMSG(("move\n")); DBGMSG(("move\n"));
int size = color ? width * 3 : in_size; int size = color ? width * 3 : in_size;
@@ -281,7 +278,7 @@ PSDriver::NextBand(BBitmap* bitmap, BPoint* offset)
for (int i = rc.top; i <= rc.bottom; i++) { for (int i = rc.top; i <= rc.bottom; i++) {
if (color) { if (color) {
uchar* out = out_buffer; uchar* out = &out_buffer[0];
uchar* in = ptr; uchar* in = ptr;
for (int w = width; w > 0; w --) { for (int w = width; w > 0; w --) {
*out++ = ToHexDigit((in[2]) >> 4); *out++ = ToHexDigit((in[2]) >> 4);
@@ -293,10 +290,10 @@ PSDriver::NextBand(BBitmap* bitmap, BPoint* offset)
in += 4; in += 4;
} }
} else { } else {
fHalftone->Dither(in_buffer, ptr, x, y, width); fHalftone->Dither(&in_buffer[0], ptr, x, y, width);
uchar* in = in_buffer; uchar* in = &in_buffer[0];
uchar* out = out_buffer; uchar* out = &out_buffer[0];
for (int w = in_size; w > 0; w --, in ++) { for (int w = in_size; w > 0; w --, in ++) {
*in = ~*in; // invert pixels *in = ~*in; // invert pixels
@@ -307,7 +304,7 @@ PSDriver::NextBand(BBitmap* bitmap, BPoint* offset)
{ {
compression_method = 0; // uncompressed compression_method = 0; // uncompressed
buffer = out_buffer; buffer = &out_buffer[0];
compressed_size = out_size; compressed_size = out_size;
} }