use &sapi:✌️:NullPtr instead of sapi:✌️:NullPtr().PthBoth() as NULL inside the sandbox; remove unused wrapper for png_write_end

This commit is contained in:
Alexandra Latysheva 2020-10-30 11:17:45 +00:00
parent bcecc9e74a
commit 7b6b2d2fe8
6 changed files with 21 additions and 36 deletions

View File

@ -51,19 +51,18 @@ absl::Status LibPNGMain(const std::string& infile, const std::string& outfile) {
free(buffer); free(buffer);
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_ASSIGN_OR_RETURN( sapi::v::NullPtr null = sapi::v::NullPtr();
result, api.png_image_finish_read( SAPI_ASSIGN_OR_RETURN(result,
image.PtrBoth(), sapi::v::NullPtr().PtrBoth(), api.png_image_finish_read(image.PtrBoth(), &null,
buffer_.PtrBoth(), 0, sapi::v::NullPtr().PtrBoth())); 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));
} }
SAPI_ASSIGN_OR_RETURN( SAPI_ASSIGN_OR_RETURN(result, api.png_image_write_to_file(
result, api.png_image_write_to_file(
image.PtrBoth(), outfile_var.PtrBefore(), 0, image.PtrBoth(), outfile_var.PtrBefore(), 0,
buffer_.PtrBoth(), 0, sapi::v::NullPtr().PtrBoth())); 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));
@ -87,4 +86,3 @@ int main(int argc, const char** argv) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -65,10 +65,10 @@ absl::Status ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
absl::StatusOr<png_structp> status_or_png_structp; absl::StatusOr<png_structp> status_or_png_structp;
sapi::v::ConstCStr ver_string_var(PNG_LIBPNG_VER_STRING); sapi::v::ConstCStr ver_string_var(PNG_LIBPNG_VER_STRING);
sapi::v::NullPtr null = sapi::v::NullPtr();
SAPI_ASSIGN_OR_RETURN( SAPI_ASSIGN_OR_RETURN(
status_or_png_structp, status_or_png_structp,
api.png_create_read_struct_wrapper(ver_string_var.PtrBefore(), api.png_create_read_struct_wrapper(ver_string_var.PtrBefore(), &null));
sapi::v::NullPtr().PtrBoth()));
sapi::v::RemotePtr struct_ptr(status_or_png_structp.value()); sapi::v::RemotePtr struct_ptr(status_or_png_structp.value());
if (!struct_ptr.GetValue()) { if (!struct_ptr.GetValue()) {
@ -143,10 +143,10 @@ absl::Status WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
absl::StatusOr<png_structp> status_or_png_structp; absl::StatusOr<png_structp> status_or_png_structp;
sapi::v::ConstCStr ver_string_var(PNG_LIBPNG_VER_STRING); sapi::v::ConstCStr ver_string_var(PNG_LIBPNG_VER_STRING);
sapi::v::NullPtr null = sapi::v::NullPtr();
SAPI_ASSIGN_OR_RETURN( SAPI_ASSIGN_OR_RETURN(
status_or_png_structp, status_or_png_structp,
api.png_create_write_struct_wrapper(ver_string_var.PtrBefore(), api.png_create_write_struct_wrapper(ver_string_var.PtrBefore(), &null));
sapi::v::NullPtr().PtrBoth()));
sapi::v::RemotePtr struct_ptr(status_or_png_structp.value()); sapi::v::RemotePtr struct_ptr(status_or_png_structp.value());
if (!struct_ptr.GetValue()) { if (!struct_ptr.GetValue()) {
@ -176,7 +176,6 @@ absl::Status WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
SAPI_RETURN_IF_ERROR(api.png_write_image_wrapper( SAPI_RETURN_IF_ERROR(api.png_write_image_wrapper(
&struct_ptr, d.row_pointers->PtrBefore(), d.height, d.rowbytes)); &struct_ptr, d.row_pointers->PtrBefore(), d.height, d.rowbytes));
auto null = sapi::v::NullPtr();
SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr)); SAPI_RETURN_IF_ERROR(api.png_setjmp(&struct_ptr));
SAPI_RETURN_IF_ERROR(api.png_write_end(&struct_ptr, &null)); SAPI_RETURN_IF_ERROR(api.png_write_end(&struct_ptr, &null));
@ -236,4 +235,3 @@ int main(int argc, const char** argv) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -64,9 +64,9 @@ TEST(SandboxTest, ReadWrite) {
<< "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()));
status_or_int = api.png_image_finish_read( sapi::v::NullPtr null = sapi::v::NullPtr();
image.PtrBoth(), sapi::v::NullPtr().PtrBoth(), buffer_.PtrBoth(), 0, status_or_int = api.png_image_finish_read(image.PtrBoth(), &null,
sapi::v::NullPtr().PtrBoth()); buffer_.PtrBoth(), 0, &null);
ASSERT_THAT(status_or_int, IsOk()) << "png_image_finish_read fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "png_image_finish_read fatal error";
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;
@ -76,8 +76,7 @@ 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, image.PtrBoth(), outfile_var.PtrBefore(), 0, buffer_.PtrBoth(), 0, &null);
sapi::v::NullPtr().PtrBoth());
ASSERT_THAT(status_or_int, IsOk()) << "png_image_write_to_file fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "png_image_write_to_file fatal error";
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;
@ -88,4 +87,3 @@ TEST(SandboxTest, ReadWrite) {
} }
} // namespace } // namespace

View File

@ -69,9 +69,9 @@ void ReadPng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
ASSERT_THAT(status_or_int.value(), Eq(0)) << infile << " is not a PNG file"; ASSERT_THAT(status_or_int.value(), Eq(0)) << infile << " is not a PNG file";
sapi::v::ConstCStr ver_string_var(PNG_LIBPNG_VER_STRING); sapi::v::ConstCStr ver_string_var(PNG_LIBPNG_VER_STRING);
sapi::v::NullPtr null = sapi::v::NullPtr();
absl::StatusOr<png_structp> status_or_png_structp = absl::StatusOr<png_structp> status_or_png_structp =
api.png_create_read_struct_wrapper(ver_string_var.PtrBefore(), api.png_create_read_struct_wrapper(ver_string_var.PtrBefore(), &null);
sapi::v::NullPtr().PtrBoth());
ASSERT_THAT(status_or_png_structp, IsOk()); ASSERT_THAT(status_or_png_structp, IsOk());
sapi::v::RemotePtr struct_ptr(status_or_png_structp.value()); sapi::v::RemotePtr struct_ptr(status_or_png_structp.value());
@ -151,9 +151,9 @@ void WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
ASSERT_THAT(file.GetValue(), NotNull()) << "Could not open " << outfile; ASSERT_THAT(file.GetValue(), NotNull()) << "Could not open " << outfile;
sapi::v::ConstCStr ver_string_var(PNG_LIBPNG_VER_STRING); sapi::v::ConstCStr ver_string_var(PNG_LIBPNG_VER_STRING);
sapi::v::NullPtr null = sapi::v::NullPtr();
absl::StatusOr<png_structp> status_or_png_structp = absl::StatusOr<png_structp> status_or_png_structp =
api.png_create_write_struct_wrapper(ver_string_var.PtrBefore(), api.png_create_write_struct_wrapper(ver_string_var.PtrBefore(), &null);
sapi::v::NullPtr().PtrBoth());
ASSERT_THAT(status_or_png_structp, IsOk()); ASSERT_THAT(status_or_png_structp, IsOk());
sapi::v::RemotePtr struct_ptr(status_or_png_structp.value()); sapi::v::RemotePtr struct_ptr(status_or_png_structp.value());
@ -186,7 +186,6 @@ void WritePng(LibPNGApi& api, LibPNGSapiSandbox& sandbox,
data.height, data.rowbytes), data.height, data.rowbytes),
IsOk()); IsOk());
auto null = sapi::v::NullPtr();
ASSERT_THAT(api.png_setjmp(&struct_ptr), IsOk()); ASSERT_THAT(api.png_setjmp(&struct_ptr), IsOk());
ASSERT_THAT(api.png_write_end(&struct_ptr, &null), IsOk()); ASSERT_THAT(api.png_write_end(&struct_ptr, &null), IsOk());
@ -245,4 +244,3 @@ TEST(SandboxTest, ReadModifyWrite) {
} }
} // namespace } // namespace

View File

@ -50,8 +50,3 @@ void png_write_image_wrapper(png_structrp png_ptr, png_bytep image,
png_write_image(png_ptr, ptrs); png_write_image(png_ptr, ptrs);
free(ptrs); free(ptrs);
} }
void png_write_end_wrapper(png_structrp png_ptr) {
png_write_end(png_ptr, NULL);
}

View File

@ -18,7 +18,5 @@ void png_read_image_wrapper(png_structrp png_ptr, png_bytep image,
size_t height, size_t rowbytes); size_t height, size_t rowbytes);
void png_write_image_wrapper(png_structrp png_ptr, png_bytep image, void png_write_image_wrapper(png_structrp png_ptr, png_bytep image,
size_t height, size_t rowbytes); size_t height, size_t rowbytes);
void png_write_end_wrapper(png_structrp png_ptr);
} }
#endif #endif