remove default constructors; reword some errors; skip check is there is enough free space

This commit is contained in:
Alexandra Latysheva 2020-10-30 13:27:25 +00:00
parent 36c816c693
commit 9068b28d28
5 changed files with 11 additions and 21 deletions

View File

@ -42,19 +42,12 @@ absl::Status LibPNGMain(const std::string& infile, const std::string& outfile) {
image.mutable_data()->format = PNG_FORMAT_RGBA; image.mutable_data()->format = PNG_FORMAT_RGBA;
auto buffer = malloc(PNG_IMAGE_SIZE(*image.mutable_data())); sapi::v::Array<uint8_t> buffer(PNG_IMAGE_SIZE(*image.mutable_data()));
if (!buffer) {
SAPI_RETURN_IF_ERROR(api.png_image_free(image.PtrBoth()));
return absl::OkStatus();
}
free(buffer);
sapi::v::Array<uint8_t> buffer_(PNG_IMAGE_SIZE(*image.mutable_data()));
sapi::v::NullPtr null = sapi::v::NullPtr(); sapi::v::NullPtr null = sapi::v::NullPtr();
SAPI_ASSIGN_OR_RETURN(result, SAPI_ASSIGN_OR_RETURN(result,
api.png_image_finish_read(image.PtrBoth(), &null, api.png_image_finish_read(image.PtrBoth(), &null,
buffer_.PtrBoth(), 0, &null)); buffer.PtrBoth(), 0, &null));
if (!result) { if (!result) {
return absl::InternalError( return absl::InternalError(
absl::StrCat("finish read error: ", image.mutable_data()->message)); absl::StrCat("finish read error: ", image.mutable_data()->message));
@ -62,7 +55,7 @@ absl::Status LibPNGMain(const std::string& infile, const std::string& outfile) {
SAPI_ASSIGN_OR_RETURN(result, api.png_image_write_to_file( SAPI_ASSIGN_OR_RETURN(result, api.png_image_write_to_file(
image.PtrBoth(), outfile_var.PtrBefore(), 0, image.PtrBoth(), outfile_var.PtrBefore(), 0,
buffer_.PtrBoth(), 0, &null)); buffer.PtrBoth(), 0, &null));
if (!result) { if (!result) {
return absl::InternalError( return absl::InternalError(
absl::StrCat("write error: ", image.mutable_data()->message)); absl::StrCat("write error: ", image.mutable_data()->message));

View File

@ -19,8 +19,6 @@
#include "../tests/libpng.h" // NOLINT(build/include) #include "../tests/libpng.h" // NOLINT(build/include)
struct Data { struct Data {
Data() {}
int width; int width;
int height; int height;
uint8_t color_type; uint8_t color_type;

View File

@ -61,4 +61,3 @@ class LibPNGSapiSandbox : public LibPNGSandbox {
}; };
} // namespace } // namespace

View File

@ -54,7 +54,7 @@ TEST(SandboxTest, ReadWrite) {
absl::StatusOr<int> status_or_int = api.png_image_begin_read_from_file( absl::StatusOr<int> status_or_int = api.png_image_begin_read_from_file(
image.PtrBoth(), infile_var.PtrBefore()); image.PtrBoth(), infile_var.PtrBefore());
ASSERT_THAT(status_or_int, IsOk()) ASSERT_THAT(status_or_int, IsOk())
<< "png_image_begin_read_from_file fatal error"; << "fatal error when invoking png_image_begin_read_from_file";
ASSERT_THAT(status_or_int.value(), IsTrue()) ASSERT_THAT(status_or_int.value(), IsTrue())
<< "png_image_begin_read_from_file failed: " << "png_image_begin_read_from_file failed: "
<< image.mutable_data()->message; << image.mutable_data()->message;
@ -63,11 +63,12 @@ TEST(SandboxTest, ReadWrite) {
ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION)) ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION))
<< "image version changed"; << "image version changed";
sapi::v::Array<uint8_t> buffer_(PNG_IMAGE_SIZE(*image.mutable_data())); sapi::v::Array<uint8_t> buffer(PNG_IMAGE_SIZE(*image.mutable_data()));
sapi::v::NullPtr null = sapi::v::NullPtr(); sapi::v::NullPtr null = sapi::v::NullPtr();
status_or_int = api.png_image_finish_read(image.PtrBoth(), &null, status_or_int = api.png_image_finish_read(image.PtrBoth(), &null,
buffer_.PtrBoth(), 0, &null); buffer.PtrBoth(), 0, &null);
ASSERT_THAT(status_or_int, IsOk()) << "png_image_finish_read fatal error"; ASSERT_THAT(status_or_int, IsOk())
<< "fatal error when invoking png_image_finish_read";
ASSERT_THAT(status_or_int.value(), IsTrue()) ASSERT_THAT(status_or_int.value(), IsTrue())
<< "png_image_finish_read failed: " << image.mutable_data()->message; << "png_image_finish_read failed: " << image.mutable_data()->message;
ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION)) ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION))
@ -76,8 +77,9 @@ TEST(SandboxTest, ReadWrite) {
<< "image format changed"; << "image format changed";
status_or_int = api.png_image_write_to_file( status_or_int = api.png_image_write_to_file(
image.PtrBoth(), outfile_var.PtrBefore(), 0, buffer_.PtrBoth(), 0, &null); image.PtrBoth(), outfile_var.PtrBefore(), 0, buffer.PtrBoth(), 0, &null);
ASSERT_THAT(status_or_int, IsOk()) << "png_image_write_to_file fatal error"; ASSERT_THAT(status_or_int, IsOk())
<< "fatal error when invoking png_image_write_to_file";
ASSERT_THAT(status_or_int.value(), IsTrue()) ASSERT_THAT(status_or_int.value(), IsTrue())
<< "png_image_finish_read failed: " << image.mutable_data()->message; << "png_image_finish_read failed: " << image.mutable_data()->message;
ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION)) ASSERT_THAT(image.mutable_data()->version, Eq(PNG_IMAGE_VERSION))

View File

@ -32,8 +32,6 @@ using ::testing::IsTrue;
using ::testing::NotNull; using ::testing::NotNull;
struct Data { struct Data {
Data() {}
int width; int width;
int height; int height;
uint8_t color_type; uint8_t color_type;