Skip to content

Commit

Permalink
Use ALOG methods to print messages from jpeg and image_io
Browse files Browse the repository at this point in the history
- Override output_message method for jpeg decoder
- Use SetMessageWriter() while calling Jpeg scanner from image_io

This ensures that logs are written using ALOG routines and can be
suppressed for fuzzer runs easily.
  • Loading branch information
harishdm committed Oct 4, 2023
1 parent 66cc9e8 commit 02a1a99
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions jpegdecoderhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ static void jpegrerror_exit(j_common_ptr cinfo) {
longjmp(err->setjmp_buffer, 1);
}

static void output_message(j_common_ptr cinfo) {
char buffer[JMSG_LENGTH_MAX];

/* Create the message */
(*cinfo->err->format_message)(cinfo, buffer);
ALOGE("%s\n", buffer);
}

JpegDecoderHelper::JpegDecoderHelper() {}

JpegDecoderHelper::~JpegDecoderHelper() {}
Expand Down Expand Up @@ -163,6 +171,7 @@ bool JpegDecoderHelper::extractEXIF(const void* image, int length) {

cinfo.err = jpeg_std_error(&myerr.pub);
myerr.pub.error_exit = jpegrerror_exit;
myerr.pub.output_message = output_message;

if (setjmp(myerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
Expand Down Expand Up @@ -209,6 +218,8 @@ bool JpegDecoderHelper::decode(const void* image, int length, bool decodeToRGBA)
jpegrerror_mgr myerr;
cinfo.err = jpeg_std_error(&myerr.pub);
myerr.pub.error_exit = jpegrerror_exit;
myerr.pub.output_message = output_message;

if (setjmp(myerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
return false;
Expand Down Expand Up @@ -342,6 +353,8 @@ bool JpegDecoderHelper::getCompressedImageParameters(const void* image, int leng
jpegrerror_mgr myerr;
cinfo.err = jpeg_std_error(&myerr.pub);
myerr.pub.error_exit = jpegrerror_exit;
myerr.pub.output_message = output_message;

if (setjmp(myerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
return false;
Expand Down
11 changes: 11 additions & 0 deletions jpegr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ int GetCPUCoreCount() {
return cpuCoreCount;
}

/*
* MessageWriter implementation for ALOG functions.
*/
class AlogMessageWriter : public MessageWriter {
public:
void WriteMessage(const Message& message) override {
ALOGD(GetFormattedMessage(message));
}
};

/*
* Helper function copies the JPEG image from without EXIF.
*
Expand Down Expand Up @@ -1122,6 +1132,7 @@ status_t JpegR::extractPrimaryImageAndGainMap(jr_compressed_ptr jpegr_image_ptr,
}

MessageHandler msg_handler;
msg_handler.SetMessageWriter(make_unique<AlogMessageWriter>(AlogMessageWriter()));
std::shared_ptr<DataSegment> seg =
DataSegment::Create(DataRange(0, jpegr_image_ptr->length),
static_cast<const uint8_t*>(jpegr_image_ptr->data),
Expand Down

0 comments on commit 02a1a99

Please sign in to comment.