1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
// Copyright (C) 2004-2024 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "mupdf/fitz.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef _WIN32
#ifndef NDEBUG
#define USE_OUTPUT_DEBUG_STRING
#include <windows.h>
#endif
#endif
#ifdef __ANDROID__
#define USE_ANDROID_LOG
#include <android/log.h>
#endif
void fz_default_error_callback(void *user, const char *message)
{
/* TODO: send errcode and format it here instead of in fz_report_error */
fputs(message, stderr);
fputc('\n', stderr);
#ifdef USE_OUTPUT_DEBUG_STRING
OutputDebugStringA(message);
OutputDebugStringA("\n");
#endif
#ifdef USE_ANDROID_LOG
__android_log_print(ANDROID_LOG_ERROR, "libmupdf", "%s", message);
#endif
}
void fz_default_warning_callback(void *user, const char *message)
{
fprintf(stderr, "warning: %s\n", message);
#ifdef USE_OUTPUT_DEBUG_STRING
OutputDebugStringA("warning: ");
OutputDebugStringA(message);
OutputDebugStringA("\n");
#endif
#ifdef USE_ANDROID_LOG
__android_log_print(ANDROID_LOG_WARN, "libmupdf", "%s", message);
#endif
}
/* Warning context */
void fz_set_warning_callback(fz_context *ctx, fz_warning_cb *warning_cb, void *user)
{
ctx->warn.print_user = user;
ctx->warn.print = warning_cb;
}
fz_warning_cb *fz_warning_callback(fz_context *ctx, void **user)
{
if (user)
*user = ctx->warn.print_user;
return ctx->warn.print;
}
void fz_var_imp(void *var)
{
/* Do nothing */
}
void fz_flush_warnings(fz_context *ctx)
{
if (ctx->warn.count > 1)
{
char buf[50];
fz_snprintf(buf, sizeof buf, "... repeated %d times...", ctx->warn.count);
if (ctx->warn.print)
ctx->warn.print(ctx->warn.print_user, buf);
}
ctx->warn.message[0] = 0;
ctx->warn.count = 0;
}
void (fz_vwarn)(fz_context *ctx, const char *fmt, va_list ap)
{
char buf[sizeof ctx->warn.message];
fz_vsnprintf(buf, sizeof buf, fmt, ap);
buf[sizeof(buf) - 1] = 0;
if (!strcmp(buf, ctx->warn.message))
{
ctx->warn.count++;
}
else
{
fz_flush_warnings(ctx);
if (ctx->warn.print)
ctx->warn.print(ctx->warn.print_user, buf);
fz_strlcpy(ctx->warn.message, buf, sizeof ctx->warn.message);
ctx->warn.count = 1;
}
}
void (fz_warn)(fz_context *ctx, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fz_vwarn(ctx, fmt, ap);
va_end(ap);
}
#if FZ_VERBOSE_EXCEPTIONS
void fz_vwarnFL(fz_context *ctx, const char *file, int line, const char *fmt, va_list ap)
{
char buf[sizeof ctx->warn.message];
fz_vsnprintf(buf, sizeof buf, fmt, ap);
buf[sizeof(buf) - 1] = 0;
if (!strcmp(buf, ctx->warn.message))
{
ctx->warn.count++;
}
else
{
fz_flush_warnings(ctx);
if (ctx->warn.print)
ctx->warn.print(ctx->warn.print_user, buf);
fz_strlcpy(ctx->warn.message, buf, sizeof ctx->warn.message);
ctx->warn.count = 1;
}
}
void fz_warnFL(fz_context *ctx, const char *file, int line, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fz_vwarnFL(ctx, file, line, fmt, ap);
va_end(ap);
}
#endif
/* Error context */
void fz_set_error_callback(fz_context *ctx, fz_error_cb *error_cb, void *user)
{
ctx->error.print_user = user;
ctx->error.print = error_cb;
}
fz_error_cb *fz_error_callback(fz_context *ctx, void **user)
{
if (user)
*user = ctx->error.print_user;
return ctx->error.print;
}
/* When we first setjmp, state is set to 0. Whenever we throw, we add 2 to
* this state. Whenever we enter the always block, we add 1.
*
* fz_push_try sets state to 0.
* If (fz_throw called within fz_try)
* fz_throw makes state = 2.
* If (no always block present)
* enter catch region with state = 2. OK.
* else
* fz_always entered as state < 3; Makes state = 3;
* if (fz_throw called within fz_always)
* fz_throw makes state = 5
* fz_always is not reentered.
* catch region entered with state = 5. OK.
* else
* catch region entered with state = 3. OK
* else
* if (no always block present)
* catch region not entered as state = 0. OK.
* else
* fz_always entered as state < 3. makes state = 1
* if (fz_throw called within fz_always)
* fz_throw makes state = 3;
* fz_always NOT entered as state >= 3
* catch region entered with state = 3. OK.
* else
* catch region entered with state = 1.
*/
FZ_NORETURN static void throw(fz_context *ctx, int code)
{
if (ctx->error.top > ctx->error.stack_base)
{
ctx->error.top->state += 2;
if (ctx->error.top->code != FZ_ERROR_NONE)
fz_warn(ctx, "clobbering previous error code and message (throw in always block?)");
ctx->error.top->code = code;
fz_longjmp(ctx->error.top->buffer, 1);
}
else
{
fz_flush_warnings(ctx);
if (ctx->error.print)
ctx->error.print(ctx->error.print_user, "aborting process from uncaught error!");
exit(EXIT_FAILURE);
}
}
fz_jmp_buf *fz_push_try(fz_context *ctx)
{
/* If we would overflow the exception stack, throw an exception instead
* of entering the try block. We assume that we always have room for
* 1 extra level on the stack here - i.e. we throw the error on us
* starting to use the last level. */
if (ctx->error.top + 2 >= ctx->error.stack_base + nelem(ctx->error.stack))
{
fz_strlcpy(ctx->error.message, "exception stack overflow!", sizeof ctx->error.message);
fz_flush_warnings(ctx);
if (ctx->error.print)
ctx->error.print(ctx->error.print_user, ctx->error.message);
/* We need to arrive in the always/catch block as if throw had taken place. */
ctx->error.top++;
ctx->error.top->state = 2;
ctx->error.top->code = FZ_ERROR_LIMIT;
}
else
{
ctx->error.top++;
ctx->error.top->state = 0;
ctx->error.top->code = FZ_ERROR_NONE;
}
return &ctx->error.top->buffer;
}
int fz_do_try(fz_context *ctx)
{
#ifdef __COVERITY__
return 1;
#else
return ctx->error.top->state == 0;
#endif
}
int fz_do_always(fz_context *ctx)
{
#ifdef __COVERITY__
return 1;
#else
if (ctx->error.top->state < 3)
{
ctx->error.top->state++;
return 1;
}
return 0;
#endif
}
int (fz_do_catch)(fz_context *ctx)
{
ctx->error.errcode = ctx->error.top->code;
return (ctx->error.top--)->state > 1;
}
int fz_caught(fz_context *ctx)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
return ctx->error.errcode;
}
int fz_caught_errno(fz_context *ctx)
{
assert(ctx && ctx->error.errcode == FZ_ERROR_SYSTEM);
return ctx->error.errnum;
}
const char *fz_caught_message(fz_context *ctx)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
return ctx->error.message;
}
void (fz_log_error_printf)(fz_context *ctx, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(fz_vlog_error_printf)(ctx, fmt, ap);
va_end(ap);
}
void (fz_vlog_error_printf)(fz_context *ctx, const char *fmt, va_list ap)
{
char message[256];
fz_flush_warnings(ctx);
if (ctx->error.print)
{
fz_vsnprintf(message, sizeof message, fmt, ap);
message[sizeof(message) - 1] = 0;
ctx->error.print(ctx->error.print_user, message);
}
}
void (fz_log_error)(fz_context *ctx, const char *str)
{
fz_flush_warnings(ctx);
if (ctx->error.print)
ctx->error.print(ctx->error.print_user, str);
}
/* coverity[+kill] */
FZ_NORETURN void (fz_vthrow)(fz_context *ctx, int code, const char *fmt, va_list ap)
{
if (ctx->error.errcode)
{
fz_flush_warnings(ctx);
fz_warn(ctx, "UNHANDLED EXCEPTION!");
fz_report_error(ctx);
#ifdef CLUSTER
abort();
#endif
}
if (code == FZ_ERROR_SYSTEM)
ctx->error.errnum = errno;
else
ctx->error.errnum = 0;
fz_vsnprintf(ctx->error.message, sizeof ctx->error.message, fmt, ap);
ctx->error.message[sizeof(ctx->error.message) - 1] = 0;
throw(ctx, code);
}
/* coverity[+kill] */
FZ_NORETURN void (fz_throw)(fz_context *ctx, int code, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fz_vthrow(ctx, code, fmt, ap);
va_end(ap);
}
/* coverity[+kill] */
FZ_NORETURN void (fz_rethrow)(fz_context *ctx)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
throw(ctx, ctx->error.errcode);
}
void (fz_morph_error)(fz_context *ctx, int fromerr, int toerr)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
if (ctx->error.errcode == fromerr)
ctx->error.errcode = toerr;
}
void (fz_rethrow_if)(fz_context *ctx, int err)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
if (ctx->error.errcode == err)
fz_rethrow(ctx);
}
void (fz_rethrow_unless)(fz_context *ctx, int err)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
if (ctx->error.errcode != err)
fz_rethrow(ctx);
}
static const char *
fz_error_type_name(enum fz_error_type exc)
{
switch (exc)
{
case FZ_ERROR_NONE: return "none";
case FZ_ERROR_GENERIC: return "generic";
case FZ_ERROR_SYSTEM: return "system";
case FZ_ERROR_LIBRARY: return "library";
case FZ_ERROR_UNSUPPORTED: return "unsupported";
case FZ_ERROR_ARGUMENT: return "argument";
case FZ_ERROR_LIMIT: return "limit";
case FZ_ERROR_FORMAT: return "format";
case FZ_ERROR_SYNTAX: return "syntax";
case FZ_ERROR_TRYLATER: return "trylater";
case FZ_ERROR_ABORT: return "abort";
case FZ_ERROR_REPAIRED: return "repaired";
}
return "invalid error type";
}
#if FZ_VERBOSE_EXCEPTIONS
int fz_do_catchFL(fz_context *ctx, const char *file, int line)
{
int rc = (fz_do_catch)(ctx);
if (rc)
(fz_log_error_printf)(ctx, "%s:%d: Catching", file, line);
return rc;
}
void fz_log_error_printfFL(fz_context *ctx, const char *file, int line, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fz_vlog_error_printfFL(ctx, file, line, fmt, ap);
va_end(ap);
}
void fz_vlog_error_printfFL(fz_context *ctx, const char *file, int line, const char *fmt, va_list ap)
{
char message[256];
fz_flush_warnings(ctx);
if (ctx->error.print)
{
fz_vsnprintf(message, sizeof message, fmt, ap);
message[sizeof(message) - 1] = 0;
fz_log_errorFL(ctx, file, line, message);
}
}
void fz_log_errorFL(fz_context *ctx, const char *file, int line, const char *str)
{
char message[256];
fz_flush_warnings(ctx);
if (ctx->error.print)
{
fz_snprintf(message, sizeof message, "%s:%d '%s'", file, line, str);
message[sizeof(message) - 1] = 0;
ctx->error.print(ctx->error.print_user, message);
}
}
/* coverity[+kill] */
FZ_NORETURN void fz_vthrowFL(fz_context *ctx, const char *file, int line, int code, const char *fmt, va_list ap)
{
if (ctx->error.errcode)
{
fz_flush_warnings(ctx);
fz_warn(ctx, "UNHANDLED EXCEPTION!");
fz_report_error(ctx);
#ifdef CLUSTER
abort();
#endif
}
fz_vsnprintf(ctx->error.message, sizeof ctx->error.message, fmt, ap);
ctx->error.message[sizeof(ctx->error.message) - 1] = 0;
(fz_log_error_printf)(ctx, "%s:%d: Throwing %s '%s'", file, line, fz_error_type_name(code), ctx->error.message);
throw(ctx, code);
}
/* coverity[+kill] */
FZ_NORETURN void fz_throwFL(fz_context *ctx, const char *file, int line, int code, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fz_vthrowFL(ctx, file, line, code, fmt, ap);
va_end(ap);
}
/* coverity[+kill] */
FZ_NORETURN void fz_rethrowFL(fz_context *ctx, const char *file, int line)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
(fz_log_error_printf)(ctx, "%s:%d: Rethrowing", file, line);
throw(ctx, ctx->error.errcode);
}
void fz_morph_errorFL(fz_context *ctx, const char *file, int line, int fromerr, int toerr)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
if (ctx->error.errcode == fromerr)
{
(fz_log_error_printf)(ctx, "%s:%d: Morphing %s->%s", file, line, fz_error_type_name(fromerr), fz_error_type_name(toerr));
ctx->error.errcode = toerr;
}
}
void fz_rethrow_unlessFL(fz_context *ctx, const char *file, int line, int err)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
if (ctx->error.errcode != err)
{
(fz_log_error_printf)(ctx, "%s:%d: Rethrowing", file, line);
(fz_rethrow)(ctx);
}
}
void fz_rethrow_ifFL(fz_context *ctx, const char *file, int line, int err)
{
assert(ctx && ctx->error.errcode >= FZ_ERROR_NONE);
if (ctx->error.errcode == err)
{
(fz_log_error_printf)(ctx, "%s:%d: Rethrowing", file, line);
(fz_rethrow)(ctx);
}
}
#endif
void fz_start_throw_on_repair(fz_context *ctx)
{
fz_lock(ctx, FZ_LOCK_ALLOC);
ctx->throw_on_repair++;
fz_unlock(ctx, FZ_LOCK_ALLOC);
}
void fz_end_throw_on_repair(fz_context *ctx)
{
fz_lock(ctx, FZ_LOCK_ALLOC);
ctx->throw_on_repair--;
fz_unlock(ctx, FZ_LOCK_ALLOC);
}
void fz_report_error(fz_context *ctx)
{
#ifdef CLUSTER
if (ctx->error.errcode == FZ_ERROR_TRYLATER || ctx->error.errcode == FZ_ERROR_ABORT)
{
fprintf(stderr, "REPORTED ERROR THAT IS TRYLATER OR ABORT\n");
abort();
}
#endif
/* TODO: send errcode to fz_log_error instead of formatting it here */
fz_log_error_printf(ctx, "%s error: %s", fz_error_type_name(ctx->error.errcode), ctx->error.message);
ctx->error.errcode = FZ_ERROR_NONE;
}
void fz_ignore_error(fz_context *ctx)
{
#ifdef CLUSTER
if (ctx->error.errcode != FZ_ERROR_TRYLATER && ctx->error.errcode != FZ_ERROR_ABORT)
{
fprintf(stderr, "IGNORED ERROR THAT IS NOT TRYLATER OR ABORT\n");
abort();
}
#endif
ctx->error.errcode = FZ_ERROR_NONE;
}
/* Convert an error into another runtime exception. */
const char *fz_convert_error(fz_context *ctx, int *code)
{
if (code)
*code = ctx->error.errcode;
ctx->error.errcode = FZ_ERROR_NONE;
return ctx->error.message;
}
|