pngerror.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /* pngerror.c - stub functions for i/o and memory allocation
  2. *
  3. * Copyright (c) 2018 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file provides a location for all error handling. Users who
  13. * need special error handling are expected to write replacement functions
  14. * and use png_set_error_fn() to use those functions. See the instructions
  15. * at each function.
  16. */
  17. #include "pngpriv.h"
  18. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  19. static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr,
  20. png_const_charp error_message)),PNG_NORETURN);
  21. #ifdef PNG_WARNINGS_SUPPORTED
  22. static void /* PRIVATE */
  23. png_default_warning PNGARG((png_const_structrp png_ptr,
  24. png_const_charp warning_message));
  25. #endif /* WARNINGS */
  26. /* This function is called whenever there is a fatal error. This function
  27. * should not be changed. If there is a need to handle errors differently,
  28. * you should supply a replacement error function and use png_set_error_fn()
  29. * to replace the error function at run-time.
  30. */
  31. #ifdef PNG_ERROR_TEXT_SUPPORTED
  32. PNG_FUNCTION(void,PNGAPI
  33. png_error,(png_const_structrp png_ptr, png_const_charp error_message),
  34. PNG_NORETURN)
  35. {
  36. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  37. char msg[16];
  38. if (png_ptr != NULL)
  39. {
  40. if ((png_ptr->flags &
  41. (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0)
  42. {
  43. if (*error_message == PNG_LITERAL_SHARP)
  44. {
  45. /* Strip "#nnnn " from beginning of error message. */
  46. int offset;
  47. for (offset = 1; offset<15; offset++)
  48. if (error_message[offset] == ' ')
  49. break;
  50. if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0)
  51. {
  52. int i;
  53. for (i = 0; i < offset - 1; i++)
  54. msg[i] = error_message[i + 1];
  55. msg[i - 1] = '\0';
  56. error_message = msg;
  57. }
  58. else
  59. error_message += offset;
  60. }
  61. else
  62. {
  63. if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0)
  64. {
  65. msg[0] = '0';
  66. msg[1] = '\0';
  67. error_message = msg;
  68. }
  69. }
  70. }
  71. }
  72. #endif
  73. if (png_ptr != NULL && png_ptr->error_fn != NULL)
  74. (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr),
  75. error_message);
  76. /* If the custom handler doesn't exist, or if it returns,
  77. use the default handler, which will not return. */
  78. png_default_error(png_ptr, error_message);
  79. }
  80. #else
  81. PNG_FUNCTION(void,PNGAPI
  82. png_err,(png_const_structrp png_ptr),PNG_NORETURN)
  83. {
  84. /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed
  85. * erroneously as '\0', instead of the empty string "". This was
  86. * apparently an error, introduced in libpng-1.2.20, and png_default_error
  87. * will crash in this case.
  88. */
  89. if (png_ptr != NULL && png_ptr->error_fn != NULL)
  90. (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), "");
  91. /* If the custom handler doesn't exist, or if it returns,
  92. use the default handler, which will not return. */
  93. png_default_error(png_ptr, "");
  94. }
  95. #endif /* ERROR_TEXT */
  96. /* Utility to safely appends strings to a buffer. This never errors out so
  97. * error checking is not required in the caller.
  98. */
  99. size_t
  100. png_safecat(png_charp buffer, size_t bufsize, size_t pos,
  101. png_const_charp string)
  102. {
  103. if (buffer != NULL && pos < bufsize)
  104. {
  105. if (string != NULL)
  106. while (*string != '\0' && pos < bufsize-1)
  107. buffer[pos++] = *string++;
  108. buffer[pos] = '\0';
  109. }
  110. return pos;
  111. }
  112. #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED)
  113. /* Utility to dump an unsigned value into a buffer, given a start pointer and
  114. * and end pointer (which should point just *beyond* the end of the buffer!)
  115. * Returns the pointer to the start of the formatted string.
  116. */
  117. png_charp
  118. png_format_number(png_const_charp start, png_charp end, int format,
  119. png_alloc_size_t number)
  120. {
  121. int count = 0; /* number of digits output */
  122. int mincount = 1; /* minimum number required */
  123. int output = 0; /* digit output (for the fixed point format) */
  124. *--end = '\0';
  125. /* This is written so that the loop always runs at least once, even with
  126. * number zero.
  127. */
  128. while (end > start && (number != 0 || count < mincount))
  129. {
  130. static const char digits[] = "0123456789ABCDEF";
  131. switch (format)
  132. {
  133. case PNG_NUMBER_FORMAT_fixed:
  134. /* Needs five digits (the fraction) */
  135. mincount = 5;
  136. if (output != 0 || number % 10 != 0)
  137. {
  138. *--end = digits[number % 10];
  139. output = 1;
  140. }
  141. number /= 10;
  142. break;
  143. case PNG_NUMBER_FORMAT_02u:
  144. /* Expects at least 2 digits. */
  145. mincount = 2;
  146. /* FALLTHROUGH */
  147. case PNG_NUMBER_FORMAT_u:
  148. *--end = digits[number % 10];
  149. number /= 10;
  150. break;
  151. case PNG_NUMBER_FORMAT_02x:
  152. /* This format expects at least two digits */
  153. mincount = 2;
  154. /* FALLTHROUGH */
  155. case PNG_NUMBER_FORMAT_x:
  156. *--end = digits[number & 0xf];
  157. number >>= 4;
  158. break;
  159. default: /* an error */
  160. number = 0;
  161. break;
  162. }
  163. /* Keep track of the number of digits added */
  164. ++count;
  165. /* Float a fixed number here: */
  166. if ((format == PNG_NUMBER_FORMAT_fixed) && (count == 5) && (end > start))
  167. {
  168. /* End of the fraction, but maybe nothing was output? In that case
  169. * drop the decimal point. If the number is a true zero handle that
  170. * here.
  171. */
  172. if (output != 0)
  173. *--end = '.';
  174. else if (number == 0) /* and !output */
  175. *--end = '0';
  176. }
  177. }
  178. return end;
  179. }
  180. #endif
  181. #ifdef PNG_WARNINGS_SUPPORTED
  182. /* This function is called whenever there is a non-fatal error. This function
  183. * should not be changed. If there is a need to handle warnings differently,
  184. * you should supply a replacement warning function and use
  185. * png_set_error_fn() to replace the warning function at run-time.
  186. */
  187. void PNGAPI
  188. png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
  189. {
  190. int offset = 0;
  191. if (png_ptr != NULL)
  192. {
  193. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  194. if ((png_ptr->flags &
  195. (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0)
  196. #endif
  197. {
  198. if (*warning_message == PNG_LITERAL_SHARP)
  199. {
  200. for (offset = 1; offset < 15; offset++)
  201. if (warning_message[offset] == ' ')
  202. break;
  203. }
  204. }
  205. }
  206. if (png_ptr != NULL && png_ptr->warning_fn != NULL)
  207. (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr),
  208. warning_message + offset);
  209. else
  210. png_default_warning(png_ptr, warning_message + offset);
  211. }
  212. /* These functions support 'formatted' warning messages with up to
  213. * PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter
  214. * is introduced by @<number>, where 'number' starts at 1. This follows the
  215. * standard established by X/Open for internationalizable error messages.
  216. */
  217. void
  218. png_warning_parameter(png_warning_parameters p, int number,
  219. png_const_charp string)
  220. {
  221. if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
  222. (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
  223. }
  224. void
  225. png_warning_parameter_unsigned(png_warning_parameters p, int number, int format,
  226. png_alloc_size_t value)
  227. {
  228. char buffer[PNG_NUMBER_BUFFER_SIZE];
  229. png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
  230. }
  231. void
  232. png_warning_parameter_signed(png_warning_parameters p, int number, int format,
  233. png_int_32 value)
  234. {
  235. png_alloc_size_t u;
  236. png_charp str;
  237. char buffer[PNG_NUMBER_BUFFER_SIZE];
  238. /* Avoid overflow by doing the negate in a png_alloc_size_t: */
  239. u = (png_alloc_size_t)value;
  240. if (value < 0)
  241. u = ~u + 1;
  242. str = PNG_FORMAT_NUMBER(buffer, format, u);
  243. if (value < 0 && str > buffer)
  244. *--str = '-';
  245. png_warning_parameter(p, number, str);
  246. }
  247. void
  248. png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p,
  249. png_const_charp message)
  250. {
  251. /* The internal buffer is just 192 bytes - enough for all our messages,
  252. * overflow doesn't happen because this code checks! If someone figures
  253. * out how to send us a message longer than 192 bytes, all that will
  254. * happen is that the message will be truncated appropriately.
  255. */
  256. size_t i = 0; /* Index in the msg[] buffer: */
  257. char msg[192];
  258. /* Each iteration through the following loop writes at most one character
  259. * to msg[i++] then returns here to validate that there is still space for
  260. * the trailing '\0'. It may (in the case of a parameter) read more than
  261. * one character from message[]; it must check for '\0' and continue to the
  262. * test if it finds the end of string.
  263. */
  264. while (i<(sizeof msg)-1 && *message != '\0')
  265. {
  266. /* '@' at end of string is now just printed (previously it was skipped);
  267. * it is an error in the calling code to terminate the string with @.
  268. */
  269. if (p != NULL && *message == '@' && message[1] != '\0')
  270. {
  271. int parameter_char = *++message; /* Consume the '@' */
  272. static const char valid_parameters[] = "123456789";
  273. int parameter = 0;
  274. /* Search for the parameter digit, the index in the string is the
  275. * parameter to use.
  276. */
  277. while (valid_parameters[parameter] != parameter_char &&
  278. valid_parameters[parameter] != '\0')
  279. ++parameter;
  280. /* If the parameter digit is out of range it will just get printed. */
  281. if (parameter < PNG_WARNING_PARAMETER_COUNT)
  282. {
  283. /* Append this parameter */
  284. png_const_charp parm = p[parameter];
  285. png_const_charp pend = p[parameter] + (sizeof p[parameter]);
  286. /* No need to copy the trailing '\0' here, but there is no guarantee
  287. * that parm[] has been initialized, so there is no guarantee of a
  288. * trailing '\0':
  289. */
  290. while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
  291. msg[i++] = *parm++;
  292. /* Consume the parameter digit too: */
  293. ++message;
  294. continue;
  295. }
  296. /* else not a parameter and there is a character after the @ sign; just
  297. * copy that. This is known not to be '\0' because of the test above.
  298. */
  299. }
  300. /* At this point *message can't be '\0', even in the bad parameter case
  301. * above where there is a lone '@' at the end of the message string.
  302. */
  303. msg[i++] = *message++;
  304. }
  305. /* i is always less than (sizeof msg), so: */
  306. msg[i] = '\0';
  307. /* And this is the formatted message. It may be larger than
  308. * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these
  309. * are not (currently) formatted.
  310. */
  311. png_warning(png_ptr, msg);
  312. }
  313. #endif /* WARNINGS */
  314. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  315. void PNGAPI
  316. png_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
  317. {
  318. if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
  319. {
  320. # ifdef PNG_READ_SUPPORTED
  321. if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
  322. png_ptr->chunk_name != 0)
  323. png_chunk_warning(png_ptr, error_message);
  324. else
  325. # endif
  326. png_warning(png_ptr, error_message);
  327. }
  328. else
  329. {
  330. # ifdef PNG_READ_SUPPORTED
  331. if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
  332. png_ptr->chunk_name != 0)
  333. png_chunk_error(png_ptr, error_message);
  334. else
  335. # endif
  336. png_error(png_ptr, error_message);
  337. }
  338. # ifndef PNG_ERROR_TEXT_SUPPORTED
  339. PNG_UNUSED(error_message)
  340. # endif
  341. }
  342. void /* PRIVATE */
  343. png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
  344. {
  345. if ((png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) != 0)
  346. png_warning(png_ptr, error_message);
  347. else
  348. png_error(png_ptr, error_message);
  349. # ifndef PNG_ERROR_TEXT_SUPPORTED
  350. PNG_UNUSED(error_message)
  351. # endif
  352. }
  353. void /* PRIVATE */
  354. png_app_error(png_const_structrp png_ptr, png_const_charp error_message)
  355. {
  356. if ((png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) != 0)
  357. png_warning(png_ptr, error_message);
  358. else
  359. png_error(png_ptr, error_message);
  360. # ifndef PNG_ERROR_TEXT_SUPPORTED
  361. PNG_UNUSED(error_message)
  362. # endif
  363. }
  364. #endif /* BENIGN_ERRORS */
  365. #define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */
  366. #if defined(PNG_WARNINGS_SUPPORTED) || \
  367. (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
  368. /* These utilities are used internally to build an error message that relates
  369. * to the current chunk. The chunk name comes from png_ptr->chunk_name,
  370. * which is used to prefix the message. The message is limited in length
  371. * to 63 bytes. The name characters are output as hex digits wrapped in []
  372. * if the character is invalid.
  373. */
  374. #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  375. static const char png_digit[16] = {
  376. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  377. 'A', 'B', 'C', 'D', 'E', 'F'
  378. };
  379. static void /* PRIVATE */
  380. png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
  381. error_message)
  382. {
  383. png_uint_32 chunk_name = png_ptr->chunk_name;
  384. int iout = 0, ishift = 24;
  385. while (ishift >= 0)
  386. {
  387. int c = (int)(chunk_name >> ishift) & 0xff;
  388. ishift -= 8;
  389. if (isnonalpha(c) != 0)
  390. {
  391. buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
  392. buffer[iout++] = png_digit[(c & 0xf0) >> 4];
  393. buffer[iout++] = png_digit[c & 0x0f];
  394. buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET;
  395. }
  396. else
  397. {
  398. buffer[iout++] = (char)c;
  399. }
  400. }
  401. if (error_message == NULL)
  402. buffer[iout] = '\0';
  403. else
  404. {
  405. int iin = 0;
  406. buffer[iout++] = ':';
  407. buffer[iout++] = ' ';
  408. while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0')
  409. buffer[iout++] = error_message[iin++];
  410. /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */
  411. buffer[iout] = '\0';
  412. }
  413. }
  414. #endif /* WARNINGS || ERROR_TEXT */
  415. #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)
  416. PNG_FUNCTION(void,PNGAPI
  417. png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message),
  418. PNG_NORETURN)
  419. {
  420. char msg[18+PNG_MAX_ERROR_TEXT];
  421. if (png_ptr == NULL)
  422. png_error(png_ptr, error_message);
  423. else
  424. {
  425. png_format_buffer(png_ptr, msg, error_message);
  426. png_error(png_ptr, msg);
  427. }
  428. }
  429. #endif /* READ && ERROR_TEXT */
  430. #ifdef PNG_WARNINGS_SUPPORTED
  431. void PNGAPI
  432. png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
  433. {
  434. char msg[18+PNG_MAX_ERROR_TEXT];
  435. if (png_ptr == NULL)
  436. png_warning(png_ptr, warning_message);
  437. else
  438. {
  439. png_format_buffer(png_ptr, msg, warning_message);
  440. png_warning(png_ptr, msg);
  441. }
  442. }
  443. #endif /* WARNINGS */
  444. #ifdef PNG_READ_SUPPORTED
  445. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  446. void PNGAPI
  447. png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp
  448. error_message)
  449. {
  450. if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0)
  451. png_chunk_warning(png_ptr, error_message);
  452. else
  453. png_chunk_error(png_ptr, error_message);
  454. # ifndef PNG_ERROR_TEXT_SUPPORTED
  455. PNG_UNUSED(error_message)
  456. # endif
  457. }
  458. #endif
  459. #endif /* READ */
  460. void /* PRIVATE */
  461. png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
  462. {
  463. # ifndef PNG_WARNINGS_SUPPORTED
  464. PNG_UNUSED(message)
  465. # endif
  466. /* This is always supported, but for just read or just write it
  467. * unconditionally does the right thing.
  468. */
  469. # if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
  470. if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
  471. # endif
  472. # ifdef PNG_READ_SUPPORTED
  473. {
  474. if (error < PNG_CHUNK_ERROR)
  475. png_chunk_warning(png_ptr, message);
  476. else
  477. png_chunk_benign_error(png_ptr, message);
  478. }
  479. # endif
  480. # if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
  481. else if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
  482. # endif
  483. # ifdef PNG_WRITE_SUPPORTED
  484. {
  485. if (error < PNG_CHUNK_WRITE_ERROR)
  486. png_app_warning(png_ptr, message);
  487. else
  488. png_app_error(png_ptr, message);
  489. }
  490. # endif
  491. }
  492. #ifdef PNG_ERROR_TEXT_SUPPORTED
  493. #ifdef PNG_FLOATING_POINT_SUPPORTED
  494. PNG_FUNCTION(void,
  495. png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN)
  496. {
  497. # define fixed_message "fixed point overflow in "
  498. # define fixed_message_ln ((sizeof fixed_message)-1)
  499. unsigned int iin;
  500. char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT];
  501. memcpy(msg, fixed_message, fixed_message_ln);
  502. iin = 0;
  503. if (name != NULL)
  504. while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
  505. {
  506. msg[fixed_message_ln + iin] = name[iin];
  507. ++iin;
  508. }
  509. msg[fixed_message_ln + iin] = 0;
  510. png_error(png_ptr, msg);
  511. }
  512. #endif
  513. #endif
  514. #ifdef PNG_SETJMP_SUPPORTED
  515. /* This API only exists if ANSI-C style error handling is used,
  516. * otherwise it is necessary for png_default_error to be overridden.
  517. */
  518. jmp_buf* PNGAPI
  519. png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn,
  520. size_t jmp_buf_size)
  521. {
  522. /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value
  523. * and it must not change after that. Libpng doesn't care how big the
  524. * buffer is, just that it doesn't change.
  525. *
  526. * If the buffer size is no *larger* than the size of jmp_buf when libpng is
  527. * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0
  528. * semantics that this call will not fail. If the size is larger, however,
  529. * the buffer is allocated and this may fail, causing the function to return
  530. * NULL.
  531. */
  532. if (png_ptr == NULL)
  533. return NULL;
  534. if (png_ptr->jmp_buf_ptr == NULL)
  535. {
  536. png_ptr->jmp_buf_size = 0; /* not allocated */
  537. if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local))
  538. png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local;
  539. else
  540. {
  541. png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *,
  542. png_malloc_warn(png_ptr, jmp_buf_size));
  543. if (png_ptr->jmp_buf_ptr == NULL)
  544. return NULL; /* new NULL return on OOM */
  545. png_ptr->jmp_buf_size = jmp_buf_size;
  546. }
  547. }
  548. else /* Already allocated: check the size */
  549. {
  550. size_t size = png_ptr->jmp_buf_size;
  551. if (size == 0)
  552. {
  553. size = (sizeof png_ptr->jmp_buf_local);
  554. if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local)
  555. {
  556. /* This is an internal error in libpng: somehow we have been left
  557. * with a stack allocated jmp_buf when the application regained
  558. * control. It's always possible to fix this up, but for the moment
  559. * this is a png_error because that makes it easy to detect.
  560. */
  561. png_error(png_ptr, "Libpng jmp_buf still allocated");
  562. /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */
  563. }
  564. }
  565. if (size != jmp_buf_size)
  566. {
  567. png_warning(png_ptr, "Application jmp_buf size changed");
  568. return NULL; /* caller will probably crash: no choice here */
  569. }
  570. }
  571. /* Finally fill in the function, now we have a satisfactory buffer. It is
  572. * valid to change the function on every call.
  573. */
  574. png_ptr->longjmp_fn = longjmp_fn;
  575. return png_ptr->jmp_buf_ptr;
  576. }
  577. void /* PRIVATE */
  578. png_free_jmpbuf(png_structrp png_ptr)
  579. {
  580. if (png_ptr != NULL)
  581. {
  582. jmp_buf *jb = png_ptr->jmp_buf_ptr;
  583. /* A size of 0 is used to indicate a local, stack, allocation of the
  584. * pointer; used here and in png.c
  585. */
  586. if (jb != NULL && png_ptr->jmp_buf_size > 0)
  587. {
  588. /* This stuff is so that a failure to free the error control structure
  589. * does not leave libpng in a state with no valid error handling: the
  590. * free always succeeds, if there is an error it gets ignored.
  591. */
  592. if (jb != &png_ptr->jmp_buf_local)
  593. {
  594. /* Make an internal, libpng, jmp_buf to return here */
  595. jmp_buf free_jmp_buf;
  596. if (!setjmp(free_jmp_buf))
  597. {
  598. png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
  599. png_ptr->jmp_buf_size = 0; /* stack allocation */
  600. png_ptr->longjmp_fn = longjmp;
  601. png_free(png_ptr, jb); /* Return to setjmp on error */
  602. }
  603. }
  604. }
  605. /* *Always* cancel everything out: */
  606. png_ptr->jmp_buf_size = 0;
  607. png_ptr->jmp_buf_ptr = NULL;
  608. png_ptr->longjmp_fn = 0;
  609. }
  610. }
  611. #endif
  612. /* This is the default error handling function. Note that replacements for
  613. * this function MUST NOT RETURN, or the program will likely crash. This
  614. * function is used by default, or if the program supplies NULL for the
  615. * error function pointer in png_set_error_fn().
  616. */
  617. static PNG_FUNCTION(void /* PRIVATE */,
  618. png_default_error,(png_const_structrp png_ptr, png_const_charp error_message),
  619. PNG_NORETURN)
  620. {
  621. #ifdef PNG_CONSOLE_IO_SUPPORTED
  622. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  623. /* Check on NULL only added in 1.5.4 */
  624. if (error_message != NULL && *error_message == PNG_LITERAL_SHARP)
  625. {
  626. /* Strip "#nnnn " from beginning of error message. */
  627. int offset;
  628. char error_number[16];
  629. for (offset = 0; offset<15; offset++)
  630. {
  631. error_number[offset] = error_message[offset + 1];
  632. if (error_message[offset] == ' ')
  633. break;
  634. }
  635. if ((offset > 1) && (offset < 15))
  636. {
  637. error_number[offset - 1] = '\0';
  638. fprintf(stderr, "libpng error no. %s: %s",
  639. error_number, error_message + offset + 1);
  640. fprintf(stderr, PNG_STRING_NEWLINE);
  641. }
  642. else
  643. {
  644. fprintf(stderr, "libpng error: %s, offset=%d",
  645. error_message, offset);
  646. fprintf(stderr, PNG_STRING_NEWLINE);
  647. }
  648. }
  649. else
  650. #endif
  651. {
  652. fprintf(stderr, "libpng error: %s", error_message ? error_message :
  653. "undefined");
  654. fprintf(stderr, PNG_STRING_NEWLINE);
  655. }
  656. #else
  657. PNG_UNUSED(error_message) /* Make compiler happy */
  658. #endif
  659. png_longjmp(png_ptr, 1);
  660. }
  661. PNG_FUNCTION(void,PNGAPI
  662. png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN)
  663. {
  664. #ifdef PNG_SETJMP_SUPPORTED
  665. if (png_ptr != NULL && png_ptr->longjmp_fn != NULL &&
  666. png_ptr->jmp_buf_ptr != NULL)
  667. png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val);
  668. #else
  669. PNG_UNUSED(png_ptr)
  670. PNG_UNUSED(val)
  671. #endif
  672. /* If control reaches this point, png_longjmp() must not return. The only
  673. * choice is to terminate the whole process (or maybe the thread); to do
  674. * this the ANSI-C abort() function is used unless a different method is
  675. * implemented by overriding the default configuration setting for
  676. * PNG_ABORT().
  677. */
  678. PNG_ABORT();
  679. }
  680. #ifdef PNG_WARNINGS_SUPPORTED
  681. /* This function is called when there is a warning, but the library thinks
  682. * it can continue anyway. Replacement functions don't have to do anything
  683. * here if you don't want them to. In the default configuration, png_ptr is
  684. * not used, but it is passed in case it may be useful.
  685. */
  686. static void /* PRIVATE */
  687. png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message)
  688. {
  689. #ifdef PNG_CONSOLE_IO_SUPPORTED
  690. # ifdef PNG_ERROR_NUMBERS_SUPPORTED
  691. if (*warning_message == PNG_LITERAL_SHARP)
  692. {
  693. int offset;
  694. char warning_number[16];
  695. for (offset = 0; offset < 15; offset++)
  696. {
  697. warning_number[offset] = warning_message[offset + 1];
  698. if (warning_message[offset] == ' ')
  699. break;
  700. }
  701. if ((offset > 1) && (offset < 15))
  702. {
  703. warning_number[offset + 1] = '\0';
  704. fprintf(stderr, "libpng warning no. %s: %s",
  705. warning_number, warning_message + offset);
  706. fprintf(stderr, PNG_STRING_NEWLINE);
  707. }
  708. else
  709. {
  710. fprintf(stderr, "libpng warning: %s",
  711. warning_message);
  712. fprintf(stderr, PNG_STRING_NEWLINE);
  713. }
  714. }
  715. else
  716. # endif
  717. {
  718. fprintf(stderr, "libpng warning: %s", warning_message);
  719. fprintf(stderr, PNG_STRING_NEWLINE);
  720. }
  721. #else
  722. PNG_UNUSED(warning_message) /* Make compiler happy */
  723. #endif
  724. PNG_UNUSED(png_ptr) /* Make compiler happy */
  725. }
  726. #endif /* WARNINGS */
  727. /* This function is called when the application wants to use another method
  728. * of handling errors and warnings. Note that the error function MUST NOT
  729. * return to the calling routine or serious problems will occur. The return
  730. * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1)
  731. */
  732. void PNGAPI
  733. png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr,
  734. png_error_ptr error_fn, png_error_ptr warning_fn)
  735. {
  736. if (png_ptr == NULL)
  737. return;
  738. png_ptr->error_ptr = error_ptr;
  739. png_ptr->error_fn = error_fn;
  740. #ifdef PNG_WARNINGS_SUPPORTED
  741. png_ptr->warning_fn = warning_fn;
  742. #else
  743. PNG_UNUSED(warning_fn)
  744. #endif
  745. }
  746. /* This function returns a pointer to the error_ptr associated with the user
  747. * functions. The application should free any memory associated with this
  748. * pointer before png_write_destroy and png_read_destroy are called.
  749. */
  750. png_voidp PNGAPI
  751. png_get_error_ptr(png_const_structrp png_ptr)
  752. {
  753. if (png_ptr == NULL)
  754. return NULL;
  755. return ((png_voidp)png_ptr->error_ptr);
  756. }
  757. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  758. void PNGAPI
  759. png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode)
  760. {
  761. if (png_ptr != NULL)
  762. {
  763. png_ptr->flags &=
  764. ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
  765. PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
  766. }
  767. }
  768. #endif
  769. #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
  770. defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
  771. /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
  772. * possible to implement without setjmp support just so long as there is some
  773. * way to handle the error return here:
  774. */
  775. PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI
  776. png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message),
  777. PNG_NORETURN)
  778. {
  779. png_const_structrp png_ptr = png_nonconst_ptr;
  780. png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
  781. /* An error is always logged here, overwriting anything (typically a warning)
  782. * that is already there:
  783. */
  784. if (image != NULL)
  785. {
  786. png_safecat(image->message, (sizeof image->message), 0, error_message);
  787. image->warning_or_error |= PNG_IMAGE_ERROR;
  788. /* Retrieve the jmp_buf from within the png_control, making this work for
  789. * C++ compilation too is pretty tricky: C++ wants a pointer to the first
  790. * element of a jmp_buf, but C doesn't tell us the type of that.
  791. */
  792. if (image->opaque != NULL && image->opaque->error_buf != NULL)
  793. longjmp(png_control_jmp_buf(image->opaque), 1);
  794. /* Missing longjmp buffer, the following is to help debugging: */
  795. {
  796. size_t pos = png_safecat(image->message, (sizeof image->message), 0,
  797. "bad longjmp: ");
  798. png_safecat(image->message, (sizeof image->message), pos,
  799. error_message);
  800. }
  801. }
  802. /* Here on an internal programming error. */
  803. abort();
  804. }
  805. #ifdef PNG_WARNINGS_SUPPORTED
  806. void /* PRIVATE */ PNGCBAPI
  807. png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
  808. {
  809. png_const_structrp png_ptr = png_nonconst_ptr;
  810. png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
  811. /* A warning is only logged if there is no prior warning or error. */
  812. if (image->warning_or_error == 0)
  813. {
  814. png_safecat(image->message, (sizeof image->message), 0, warning_message);
  815. image->warning_or_error |= PNG_IMAGE_WARNING;
  816. }
  817. }
  818. #endif
  819. int /* PRIVATE */
  820. png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg)
  821. {
  822. volatile png_imagep image = image_in;
  823. volatile int result;
  824. volatile png_voidp saved_error_buf;
  825. jmp_buf safe_jmpbuf;
  826. /* Safely execute function(arg) with png_error returning to this function. */
  827. saved_error_buf = image->opaque->error_buf;
  828. result = setjmp(safe_jmpbuf) == 0;
  829. if (result != 0)
  830. {
  831. image->opaque->error_buf = safe_jmpbuf;
  832. result = function(arg);
  833. }
  834. image->opaque->error_buf = saved_error_buf;
  835. /* And do the cleanup prior to any failure return. */
  836. if (result == 0)
  837. png_image_free(image);
  838. return result;
  839. }
  840. #endif /* SIMPLIFIED READ || SIMPLIFIED_WRITE */
  841. #endif /* READ || WRITE */