Flutter Engine
The Flutter Engine
SkJpegPriv.h
Go to the documentation of this file.
1/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9#ifndef SkJpegPriv_DEFINED
10#define SkJpegPriv_DEFINED
11
15
16#include <setjmp.h>
17// stdio is needed for jpeglib
18#include <stdio.h>
19
20extern "C" {
21 #include "jpeglib.h" // NO_G3_REWRITE
22 #include "jerror.h" // NO_G3_REWRITE
23}
24
25/*
26 * Error handling struct
27 */
28struct skjpeg_error_mgr : public jpeg_error_mgr {
30 public:
31 AutoPushJmpBuf(skjpeg_error_mgr* mgr) : fMgr(mgr) { fMgr->push(&fJmpBuf); }
32 ~AutoPushJmpBuf() { fMgr->pop(&fJmpBuf); }
33 operator jmp_buf&() { return fJmpBuf; }
34
35 private:
36 skjpeg_error_mgr* const fMgr;
37 jmp_buf fJmpBuf;
38 };
39
40 // When libjpeg initializes the fields of a `jpeg_error_mgr` (in `jpeg_std_error`), it
41 // leaves the msg_parm fields uninitialized. This is safe, but confuses MSAN, so we
42 // explicitly zero out the structure when constructing it. (crbug.com/oss-fuzz/68691)
43 skjpeg_error_mgr() : jpeg_error_mgr({}) {}
44
45 void push(jmp_buf* j) {
46 SkASSERT(fStack[3] == nullptr); // if we assert here, the stack has overflowed
47 fStack[3] = fStack[2];
48 fStack[2] = fStack[1];
49 fStack[1] = fStack[0];
50 fStack[0] = j;
51 }
52
53 void pop(jmp_buf* j) {
54 SkASSERT(fStack[0] == j); // if we assert here, the pushes and pops were unbalanced
55 fStack[0] = fStack[1];
56 fStack[1] = fStack[2];
57 fStack[2] = fStack[3];
58 fStack[3] = nullptr;
59 }
60
61 jmp_buf* fStack[4] = {};
62};
63
64#endif
#define SkASSERT(cond)
Definition: SkAssert.h:116
AutoPushJmpBuf(skjpeg_error_mgr *mgr)
Definition: SkJpegPriv.h:31
void push(jmp_buf *j)
Definition: SkJpegPriv.h:45
void pop(jmp_buf *j)
Definition: SkJpegPriv.h:53
jmp_buf * fStack[4]
Definition: SkJpegPriv.h:61