Coverage Report

Created: 2024-06-18 06:05

/src/libjpeg-turbo/jerror.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jerror.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1998, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright (C) 2022, 2024, D. R. Commander.
8
 * For conditions of distribution and use, see the accompanying README.ijg
9
 * file.
10
 *
11
 * This file contains simple error-reporting and trace-message routines.
12
 * These are suitable for Unix-like systems and others where writing to
13
 * stderr is the right thing to do.  Many applications will want to replace
14
 * some or all of these routines.
15
 *
16
 * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
17
 * you get a Windows-specific hack to display error messages in a dialog box.
18
 * It ain't much, but it beats dropping error messages into the bit bucket,
19
 * which is what happens to output to stderr under most Windows C compilers.
20
 *
21
 * These routines are used by both the compression and decompression code.
22
 */
23
24
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
25
#include "jinclude.h"
26
#include "jpeglib.h"
27
#include "jversion.h"
28
#include "jerror.h"
29
30
#ifdef USE_WINDOWS_MESSAGEBOX
31
#include <windows.h>
32
#endif
33
34
#ifndef EXIT_FAILURE            /* define exit() codes if not provided */
35
#define EXIT_FAILURE  1
36
#endif
37
38
39
/*
40
 * Create the message string table.
41
 * We do this from the master message list in jerror.h by re-reading
42
 * jerror.h with a suitable definition for macro JMESSAGE.
43
 */
44
45
#define JMESSAGE(code, string)  string,
46
47
static const char * const jpeg_std_message_table[] = {
48
#include "jerror.h"
49
  NULL
50
};
51
52
53
/*
54
 * Error exit handler: must not return to caller.
55
 *
56
 * Applications may override this if they want to get control back after
57
 * an error.  Typically one would longjmp somewhere instead of exiting.
58
 * The setjmp buffer can be made a private field within an expanded error
59
 * handler object.  Note that the info needed to generate an error message
60
 * is stored in the error object, so you can generate the message now or
61
 * later, at your convenience.
62
 * You should make sure that the JPEG object is cleaned up (with jpeg_abort
63
 * or jpeg_destroy) at some point.
64
 */
65
66
METHODDEF(void)
67
error_exit(j_common_ptr cinfo)
68
0
{
69
  /* Always display the message */
70
0
  (*cinfo->err->output_message) (cinfo);
71
72
  /* Let the memory manager delete any temp files before we die */
73
0
  jpeg_destroy(cinfo);
74
75
0
  exit(EXIT_FAILURE);
76
0
}
77
78
79
/*
80
 * Actual output of an error or trace message.
81
 * Applications may override this method to send JPEG messages somewhere
82
 * other than stderr.
83
 *
84
 * On Windows, printing to stderr is generally completely useless,
85
 * so we provide optional code to produce an error-dialog popup.
86
 * Most Windows applications will still prefer to override this routine,
87
 * but if they don't, it'll do something at least marginally useful.
88
 *
89
 * NOTE: to use the library in an environment that doesn't support the
90
 * C stdio library, you may have to delete the call to fprintf() entirely,
91
 * not just not use this routine.
92
 */
93
94
METHODDEF(void)
95
output_message(j_common_ptr cinfo)
96
0
{
97
0
  char buffer[JMSG_LENGTH_MAX];
98
99
  /* Create the message */
100
0
  (*cinfo->err->format_message) (cinfo, buffer);
101
102
#ifdef USE_WINDOWS_MESSAGEBOX
103
  /* Display it in a message dialog box */
104
  MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
105
             MB_OK | MB_ICONERROR);
106
#else
107
  /* Send it to stderr, adding a newline */
108
0
  fprintf(stderr, "%s\n", buffer);
109
0
#endif
110
0
}
111
112
113
/*
114
 * Decide whether to emit a trace or warning message.
115
 * msg_level is one of:
116
 *   -1: recoverable corrupt-data warning, may want to abort.
117
 *    0: important advisory messages (always display to user).
118
 *    1: first level of tracing detail.
119
 *    2,3,...: successively more detailed tracing messages.
120
 * An application might override this method if it wanted to abort on warnings
121
 * or change the policy about which messages to display.
122
 */
123
124
METHODDEF(void)
125
emit_message(j_common_ptr cinfo, int msg_level)
126
0
{
127
0
  struct jpeg_error_mgr *err = cinfo->err;
128
129
0
  if (msg_level < 0) {
130
    /* It's a warning message.  Since corrupt files may generate many warnings,
131
     * the policy implemented here is to show only the first warning,
132
     * unless trace_level >= 3.
133
     */
134
0
    if (err->num_warnings == 0 || err->trace_level >= 3)
135
0
      (*err->output_message) (cinfo);
136
    /* Always count warnings in num_warnings. */
137
0
    err->num_warnings++;
138
0
  } else {
139
    /* It's a trace message.  Show it if trace_level >= msg_level. */
140
0
    if (err->trace_level >= msg_level)
141
0
      (*err->output_message) (cinfo);
142
0
  }
143
0
}
144
145
146
/*
147
 * Format a message string for the most recent JPEG error or message.
148
 * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
149
 * characters.  Note that no '\n' character is added to the string.
150
 * Few applications should need to override this method.
151
 */
152
153
METHODDEF(void)
154
format_message(j_common_ptr cinfo, char *buffer)
155
0
{
156
0
  struct jpeg_error_mgr *err = cinfo->err;
157
0
  int msg_code = err->msg_code;
158
0
  const char *msgtext = NULL;
159
0
  const char *msgptr;
160
0
  char ch;
161
0
  boolean isstring;
162
163
  /* Look up message string in proper table */
164
0
  if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
165
0
    msgtext = err->jpeg_message_table[msg_code];
166
0
  } else if (err->addon_message_table != NULL &&
167
0
             msg_code >= err->first_addon_message &&
168
0
             msg_code <= err->last_addon_message) {
169
0
    msgtext = err->addon_message_table[msg_code - err->first_addon_message];
170
0
  }
171
172
  /* Defend against bogus message number */
173
0
  if (msgtext == NULL) {
174
0
    err->msg_parm.i[0] = msg_code;
175
0
    msgtext = err->jpeg_message_table[0];
176
0
  }
177
178
  /* Check for string parameter, as indicated by %s in the message text */
179
0
  isstring = FALSE;
180
0
  msgptr = msgtext;
181
0
  while ((ch = *msgptr++) != '\0') {
182
0
    if (ch == '%') {
183
0
      if (*msgptr == 's') isstring = TRUE;
184
0
      break;
185
0
    }
186
0
  }
187
188
  /* Format the message into the passed buffer */
189
0
  if (isstring)
190
0
    SNPRINTF(buffer, JMSG_LENGTH_MAX, msgtext, err->msg_parm.s);
191
0
  else
192
0
    SNPRINTF(buffer, JMSG_LENGTH_MAX, msgtext,
193
0
             err->msg_parm.i[0], err->msg_parm.i[1],
194
0
             err->msg_parm.i[2], err->msg_parm.i[3],
195
0
             err->msg_parm.i[4], err->msg_parm.i[5],
196
0
             err->msg_parm.i[6], err->msg_parm.i[7]);
197
0
}
198
199
200
/*
201
 * Reset error state variables at start of a new image.
202
 * This is called during compression startup to reset trace/error
203
 * processing to default state, without losing any application-specific
204
 * method pointers.  An application might possibly want to override
205
 * this method if it has additional error processing state.
206
 */
207
208
METHODDEF(void)
209
reset_error_mgr(j_common_ptr cinfo)
210
0
{
211
0
  cinfo->err->num_warnings = 0;
212
  /* trace_level is not reset since it is an application-supplied parameter */
213
0
  cinfo->err->msg_code = 0;     /* may be useful as a flag for "no error" */
214
0
}
215
216
217
/*
218
 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
219
 * Typical call is:
220
 *      struct jpeg_compress_struct cinfo;
221
 *      struct jpeg_error_mgr err;
222
 *
223
 *      cinfo.err = jpeg_std_error(&err);
224
 * after which the application may override some of the methods.
225
 */
226
227
GLOBAL(struct jpeg_error_mgr *)
228
jpeg_std_error(struct jpeg_error_mgr *err)
229
0
{
230
0
  memset(err, 0, sizeof(struct jpeg_error_mgr));
231
232
0
  err->error_exit = error_exit;
233
0
  err->emit_message = emit_message;
234
0
  err->output_message = output_message;
235
0
  err->format_message = format_message;
236
0
  err->reset_error_mgr = reset_error_mgr;
237
238
  /* Initialize message table pointers */
239
0
  err->jpeg_message_table = jpeg_std_message_table;
240
0
  err->last_jpeg_message = (int)JMSG_LASTMSGCODE - 1;
241
242
0
  return err;
243
0
}