Skip to content

Commit

Permalink
Use standard C attributes
Browse files Browse the repository at this point in the history
ISO have recently standardized a way of defining attributes based on the
ISO-C++ syntax standardised in 2011.  This allows removal of the
existing compiler-specific hacks as well as compatibility with C++.  The
latter is important for attributes used in the header files which
declare the libpng API (png.h and descendants.)

This also extends the attribute handling to any compliant C compiler,
not just the three with compiler specific support in the prior code.

The fix also enables removal of the highly idiosyncratic use of specific
comments to indicate "fall through" in case statements; the concept is
now supported by the [[fallthrough]] attribute.

The changes include checks to ensure that the attribute syntax is
supported by the compiler and that the specific attributes are also
supported.

The changes are detailed in these commits:

commit 51b6189
commit 8942be9

Signed-off-by: John Bowler <jbowler@acm.org>
  • Loading branch information
jbowler committed Oct 11, 2024
1 parent 37cc20a commit f6dc7e7
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 165 deletions.
26 changes: 19 additions & 7 deletions contrib/libtests/pngstest.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* pngstest.c
*
* Copyright (c) 2021-2024 Cosmin Truta
* Copyright (c) 2013-2017 John Cunningham Bowler
* Copyright (c) 2013-2017,2024 John Cunningham Bowler
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
Expand Down Expand Up @@ -46,6 +46,18 @@
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED /* Else nothing can be done */
#include "../tools/sRGB.h"

/* This is a sanity check on PNG_HAS_ATTRIBUTE.
*/
#if PNG_HAS_ATTRIBUTE(__unknown__::attribute)
# error PNG_HAS_ATTRIBUTE is not working correctly
#endif

#if PNG_HAS_ATTRIBUTE(fallthrough)
# define FALLTHROUGH PNG_ATTRIBUTE(fallthrough)
#else
# define FALLTRHOUGH /* FALLTHROUGH */
#endif

/* KNOWN ISSUES
*
* These defines switch on alternate algorithms for format conversions to match
Expand Down Expand Up @@ -2885,13 +2897,13 @@ compare_two_images(Image *a, Image *b, int via_linear,
{
case 4:
if (pua[btoa[3]] != pub[3]) break;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
case 3:
if (pua[btoa[2]] != pub[2]) break;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
case 2:
if (pua[btoa[1]] != pub[1]) break;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
case 1:
if (pua[btoa[0]] != pub[0]) break;
if (alpha_added != 4 && pub[alpha_added] != 65535) break;
Expand All @@ -2907,13 +2919,13 @@ compare_two_images(Image *a, Image *b, int via_linear,
{
case 4:
if (psa[btoa[3]] != psb[3]) break;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
case 3:
if (psa[btoa[2]] != psb[2]) break;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
case 2:
if (psa[btoa[1]] != psb[1]) break;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
case 1:
if (psa[btoa[0]] != psb[0]) break;
if (alpha_added != 4 && psb[alpha_added] != 255) break;
Expand Down
15 changes: 11 additions & 4 deletions contrib/libtests/pngunknown.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*
* Copyright (c) 2021-2024 Cosmin Truta
* Copyright (c) 2015,2017 Glenn Randers-Pehrson
* Copyright (c) 2015-2016,2024 John Bowler
* Written by John Cunningham Bowler
*
* This code is released under the libpng license.
Expand Down Expand Up @@ -38,6 +39,12 @@
# define SKIP 0
#endif

#if PNG_HAS_ATTRIBUTE(fallthrough)
# define FALLTHROUGH PNG_ATTRIBUTE(fallthrough)
#else
# define FALLTRHOUGH /* FALLTHROUGH */
#endif


/* Since this program tests the ability to change the unknown chunk handling
* these must be defined:
Expand Down Expand Up @@ -433,7 +440,7 @@ clean_display(display *d)
}
}

PNG_FUNCTION(void, display_exit, (display *d), static PNG_NORETURN)
PNG_FUNCTION(void, display_exit, (display *d), PNG_NORETURN static)
{
++(d->error_count);

Expand All @@ -457,7 +464,7 @@ display_rc(const display *d, int strict)

/* libpng error and warning callbacks */
PNG_FUNCTION(void, (PNGCBAPI error), (png_structp png_ptr, const char *message),
static PNG_NORETURN)
PNG_NORETURN static)
{
display *d = (display*)png_get_error_ptr(png_ptr);

Expand Down Expand Up @@ -623,7 +630,7 @@ get_unknown(display *d, png_infop info_ptr, int after_IDAT)
++(d->error_count);
break;
}
/* FALLTHROUGH */ /* (safe) */
FALLTHROUGH; /* (safe) */ /* FALLTHROUGH */
case PNG_HANDLE_CHUNK_ALWAYS:
break;
}
Expand Down Expand Up @@ -1104,7 +1111,7 @@ static const char *standard_tests[] =
NULL /*end*/
};

static PNG_NORETURN void
PNG_NORETURN static void
usage(const char *program, const char *reason)
{
fprintf(stderr, "pngunknown: %s: usage:\n %s [--strict] "
Expand Down
15 changes: 11 additions & 4 deletions contrib/libtests/pngvalid.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* pngvalid.c - validate libpng by constructing then reading png files.
*
* Copyright (c) 2021-2024 Cosmin Truta
* Copyright (c) 2014-2017 John Cunningham Bowler
* Copyright (c) 2014-2017,2024 John Cunningham Bowler
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
Expand Down Expand Up @@ -156,6 +156,13 @@ typedef png_byte *png_const_bytep;
# endif
#endif

#if PNG_HAS_ATTRIBUTE(fallthrough)
# define FALLTHROUGH PNG_ATTRIBUTE(fallthrough)
#else
# define FALLTRHOUGH /* FALLTHROUGH */
#endif


/***************************** EXCEPTION HANDLING *****************************/
#ifdef PNG_FREESTANDING_TESTS
# include <cexcept.h>
Expand Down Expand Up @@ -6589,12 +6596,12 @@ transform_info_imp(transform_display *dp, png_structp pp, png_infop pi)
{
case PNG_COLOR_TYPE_PALETTE:
if (dp->output_bit_depth > 8) goto error;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
case PNG_COLOR_TYPE_GRAY:
if (dp->output_bit_depth == 1 || dp->output_bit_depth == 2 ||
dp->output_bit_depth == 4)
break;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
default:
if (dp->output_bit_depth == 8 || dp->output_bit_depth == 16)
break;
Expand Down Expand Up @@ -10001,7 +10008,7 @@ gamma_component_validate(const char *name, const validate_info *vi,
use_background = (alpha >= 0 && alpha < 1);
# endif
# ifdef PNG_READ_ALPHA_MODE_SUPPORTED
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */
case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
case ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED:
Expand Down
14 changes: 10 additions & 4 deletions contrib/tools/pngfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
# include "../../png.h"
#endif

#if PNG_HAS_ATTRIBUTE(fallthrough)
# define FALLTHROUGH PNG_ATTRIBUTE(fallthrough)
#else
# define FALLTRHOUGH /* FALLTHROUGH */
#endif

#ifdef PNG_SETJMP_SUPPORTED
#include <setjmp.h>

Expand Down Expand Up @@ -2399,7 +2405,7 @@ zlib_advance(struct zlib *zlib, png_uint_32 nbytes)
endrc = ZLIB_TOO_FAR_BACK;
break;
}
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */

default:
zlib_message(zlib, 0/*stream error*/);
Expand Down Expand Up @@ -2553,7 +2559,7 @@ zlib_run(struct zlib *zlib)
list->lengths[i] -= zlib->extra_bytes;
list->count = i+1;
zlib->idat->idat_list_tail = list;
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */

default:
return rc;
Expand Down Expand Up @@ -2656,7 +2662,7 @@ zlib_check(struct file *file, png_uint_32 offset)
/* Truncated stream; unrecoverable, gets converted to ZLIB_FATAL */
zlib.z.msg = PNGZ_MSG_CAST("[truncated]");
zlib_message(&zlib, 0/*expected*/);
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */

default:
/* Unrecoverable error; skip the chunk; a zlib_message has already
Expand Down Expand Up @@ -3324,7 +3330,7 @@ read_callback(png_structp png_ptr, png_bytep buffer, size_t count)
if (file->state != STATE_IDAT && length > 0)
setpos(chunk);
}
/* FALLTHROUGH */
FALLTHROUGH; /* FALLTHROUGH */

default:
assert(chunk != NULL);
Expand Down
Loading

0 comments on commit f6dc7e7

Please sign in to comment.