pngerror.c 28 KB

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