Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional warning flags and PNGLIB_FALLTHROUGH #572

Open
wants to merge 1 commit into
base: libpng16
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ project(libpng
VERSION ${PNGLIB_VERSION}
LANGUAGES C ASM)

if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wimplicit-fallthrough -Waddress -Wenum-compare -Winfinite-recursion -Wmisleading-indentation -Wself-assign -Wshift-sign-overflow -Wuninitialized -Wunused -Werror")
endif()

if(POLICY CMP0074)
# Allow find_package() to use the ZLIB_ROOT variable, if available.
cmake_policy(SET CMP0074 NEW)
Expand Down
14 changes: 8 additions & 6 deletions contrib/libtests/pngstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define _ISOC90_SOURCE 1
#define MALLOC_CHECK_ 2 /*glibc facility: turn on debugging*/

#include "pngpriv.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -2888,13 +2890,13 @@ compare_two_images(Image *a, Image *b, int via_linear,
{
case 4:
if (pua[btoa[3]] != pub[3]) break;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 3:
if (pua[btoa[2]] != pub[2]) break;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 2:
if (pua[btoa[1]] != pub[1]) break;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 1:
if (pua[btoa[0]] != pub[0]) break;
if (alpha_added != 4 && pub[alpha_added] != 65535) break;
Expand All @@ -2910,13 +2912,13 @@ compare_two_images(Image *a, Image *b, int via_linear,
{
case 4:
if (psa[btoa[3]] != psb[3]) break;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 3:
if (psa[btoa[2]] != psb[2]) break;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 2:
if (psa[btoa[1]] != psb[1]) break;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 1:
if (psa[btoa[0]] != psb[0]) break;
if (alpha_added != 4 && psb[alpha_added] != 255) break;
Expand Down
5 changes: 4 additions & 1 deletion contrib/libtests/pngunknown.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* currently just a minimal validation for the built-in libpng facilities.
*/

#include "pngpriv.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -103,6 +105,7 @@ typedef png_byte *png_const_bytep;
#define PNG_u2(b1, b2) (((unsigned int)(b1) << 8) + (b2))

#define PNG_U16(b1, b2) ((png_uint_16)PNG_u2(b1, b2))
#undef PNG_U32
#define PNG_U32(b1, b2, b3, b4)\
(((png_uint_32)PNG_u2(b1, b2) << 16) + PNG_u2(b3, b4))

Expand Down Expand Up @@ -627,7 +630,7 @@ get_unknown(display *d, png_infop info_ptr, int after_IDAT)
++(d->error_count);
break;
}
/* FALLTHROUGH */ /* (safe) */
PNGLIB_FALLTHROUGH; /* (safe) */
case PNG_HANDLE_CHUNK_ALWAYS:
break;
}
Expand Down
10 changes: 6 additions & 4 deletions contrib/libtests/pngvalid.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#define _BSD_SOURCE 1 /* For the floating point exception extension */
#define _DEFAULT_SOURCE 1 /* For the floating point exception extension */

#include "pngpriv.h"

#include <signal.h>
#include <stdio.h>

Expand Down Expand Up @@ -6595,16 +6597,16 @@ 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 */
PNGLIB_FALLTHROUGH;
case PNG_COLOR_TYPE_GRAY:
if (dp->output_bit_depth == 1 || dp->output_bit_depth == 2 ||
dp->output_bit_depth == 4)
break;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
default:
if (dp->output_bit_depth == 8 || dp->output_bit_depth == 16)
break;
/* FALLTHROUGH */
// Fall through to error (not a case so no PNGLIB_FALLTHROUGH)
error:
{
char message[128];
Expand Down Expand Up @@ -10007,7 +10009,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 */
PNGLIB_FALLTHROUGH;
case ALPHA_MODE_OFFSET + PNG_ALPHA_STANDARD:
case ALPHA_MODE_OFFSET + PNG_ALPHA_BROKEN:
case ALPHA_MODE_OFFSET + PNG_ALPHA_OPTIMIZED:
Expand Down
10 changes: 6 additions & 4 deletions contrib/tools/pngfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* See the usage message for more information.
*/

#include "pngpriv.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -2405,7 +2407,7 @@ zlib_advance(struct zlib *zlib, png_uint_32 nbytes)
endrc = ZLIB_TOO_FAR_BACK;
break;
}
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;

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

default:
return rc;
Expand Down Expand Up @@ -2662,7 +2664,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 */
PNGLIB_FALLTHROUGH;

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

default:
assert(chunk != NULL);
Expand Down
4 changes: 2 additions & 2 deletions pngerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ png_format_number(png_const_charp start, png_charp end, int format,
case PNG_NUMBER_FORMAT_02u:
/* Expects at least 2 digits. */
mincount = 2;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;

case PNG_NUMBER_FORMAT_u:
*--end = digits[number % 10];
Expand All @@ -173,7 +173,7 @@ png_format_number(png_const_charp start, png_charp end, int format,
case PNG_NUMBER_FORMAT_02x:
/* This format expects at least two digits */
mincount = 2;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;

case PNG_NUMBER_FORMAT_x:
*--end = digits[number & 0xf];
Expand Down
9 changes: 9 additions & 0 deletions pngpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,15 @@ PNG_INTERNAL_FUNCTION(int,

/* Maintainer: Put new private prototypes here ^ */

#if defined(__has_attribute)
# if __has_attribute(fallthrough)
# define PNGLIB_FALLTHROUGH __attribute__((fallthrough))
# endif
#endif
#if !defined(PNGLIB_FALLTHROUGH)
# define PNGLIB_FALLTHROUGH (void)0
#endif

#include "pngdebug.h"

#ifdef __cplusplus
Expand Down
10 changes: 5 additions & 5 deletions pngread.c
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ png_create_colormap_entry(png_image_read_control *display,
{
case 4:
entry[afirst ? 0 : 3] = (png_uint_16)alpha;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;

case 3:
if (alpha < 65535)
Expand All @@ -1928,7 +1928,7 @@ png_create_colormap_entry(png_image_read_control *display,

case 2:
entry[1 ^ afirst] = (png_uint_16)alpha;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;

case 1:
if (alpha < 65535)
Expand Down Expand Up @@ -1957,7 +1957,7 @@ png_create_colormap_entry(png_image_read_control *display,
{
case 4:
entry[afirst ? 0 : 3] = (png_byte)alpha;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 3:
entry[afirst + (2 ^ bgr)] = (png_byte)blue;
entry[afirst + 1] = (png_byte)green;
Expand All @@ -1966,7 +1966,7 @@ png_create_colormap_entry(png_image_read_control *display,

case 2:
entry[1 ^ afirst] = (png_byte)alpha;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 1:
entry[afirst] = (png_byte)green;
break;
Expand Down Expand Up @@ -2886,7 +2886,7 @@ png_image_read_colormap(png_voidp argument)
case P_sRGB:
/* Change to 8-bit sRGB */
png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;

case P_FILE:
if (png_ptr->bit_depth > 8)
Expand Down
4 changes: 2 additions & 2 deletions pngrtran.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action)
case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
png_warning(png_ptr,
"Can't discard critical data on CRC error");
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case PNG_CRC_ERROR_QUIT: /* Error/quit */

case PNG_CRC_DEFAULT:
Expand Down Expand Up @@ -1263,7 +1263,7 @@ png_init_rgb_transformations(png_structrp png_ptr)
default:

case 8:
/* FALLTHROUGH */ /* (Already 8 bits) */
PNGLIB_FALLTHROUGH; /* (Already 8 bits) */

case 16:
/* Already a full 16 bits */
Expand Down
4 changes: 2 additions & 2 deletions pngrutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -3071,7 +3071,7 @@ png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
case 2:
png_ptr->user_chunk_cache_max = 1;
png_chunk_benign_error(png_ptr, "no space in chunk cache");
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 1:
/* NOTE: prior to 1.6.0 this case resulted in an unknown critical
* chunk being skipped, now there will be a hard error below.
Expand All @@ -3080,7 +3080,7 @@ png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,

default: /* not at limit */
--(png_ptr->user_chunk_cache_max);
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 0: /* no limit */
# endif /* USER_LIMITS */
/* Here when the limit isn't reached or when limits are compiled
Expand Down
6 changes: 3 additions & 3 deletions pngwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ png_set_filter(png_structrp png_ptr, int method, int filters)
case 6:
case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
#endif /* WRITE_FILTER */
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case PNG_FILTER_VALUE_NONE:
png_ptr->do_filter = PNG_FILTER_NONE; break;

Expand Down Expand Up @@ -1909,7 +1909,7 @@ png_image_set_PLTE(png_image_write_control *display)
tRNS[i] = entry[afirst ? 0 : 3];
if (tRNS[i] < 255)
num_trans = i+1;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 3:
palette[i].blue = entry[afirst + (2 ^ bgr)];
palette[i].green = entry[afirst + 1];
Expand All @@ -1920,7 +1920,7 @@ png_image_set_PLTE(png_image_write_control *display)
tRNS[i] = entry[1 ^ afirst];
if (tRNS[i] < 255)
num_trans = i+1;
/* FALLTHROUGH */
PNGLIB_FALLTHROUGH;
case 1:
palette[i].blue = palette[i].red = palette[i].green =
entry[afirst];
Expand Down