Simplify the definition of png_isaligned and avoid compiler warnings

The following pointer subtraction was unnecessary:
((const char*)(ptr)-(const char*)0)

In order to avoid further warnings about casting a wide pointer type
to a narrower integer type, we cast the pointer to the target integer
type through (size_t).

Also fix a comment and reformat the surrounding code.
This commit is contained in:
Cosmin Truta 2022-09-14 11:07:36 +03:00
parent 0c2388cdae
commit e9e9801a84
2 changed files with 18 additions and 20 deletions

View File

@ -556,14 +556,14 @@
#endif #endif
/* These macros may need to be architecture dependent. */ /* These macros may need to be architecture dependent. */
#define PNG_ALIGN_NONE 0 /* do not use data alignment */ #define PNG_ALIGN_NONE 0 /* do not use data alignment */
#define PNG_ALIGN_ALWAYS 1 /* assume unaligned accesses are OK */ #define PNG_ALIGN_ALWAYS 1 /* assume unaligned accesses are OK */
#ifdef offsetof #ifdef offsetof
# define PNG_ALIGN_OFFSET 2 /* use offsetof to determine alignment */ # define PNG_ALIGN_OFFSET 2 /* use offsetof to determine alignment */
#else #else
# define PNG_ALIGN_OFFSET -1 /* prevent the use of this */ # define PNG_ALIGN_OFFSET -1 /* prevent the use of this */
#endif #endif
#define PNG_ALIGN_SIZE 3 /* use sizeof to determine alignment */ #define PNG_ALIGN_SIZE 3 /* use sizeof to determine alignment */
#ifndef PNG_ALIGN_TYPE #ifndef PNG_ALIGN_TYPE
/* Default to using aligned access optimizations and requiring alignment to a /* Default to using aligned access optimizations and requiring alignment to a
@ -577,26 +577,25 @@
/* This is used because in some compiler implementations non-aligned /* This is used because in some compiler implementations non-aligned
* structure members are supported, so the offsetof approach below fails. * structure members are supported, so the offsetof approach below fails.
* Set PNG_ALIGN_SIZE=0 for compiler combinations where unaligned access * Set PNG_ALIGN_SIZE=0 for compiler combinations where unaligned access
* is good for performance. Do not do this unless you have tested the result * is good for performance. Do not do this unless you have tested the
* and understand it. * result and understand it.
*/ */
# define png_alignof(type) (sizeof (type)) # define png_alignof(type) (sizeof(type))
#else #else
# if PNG_ALIGN_TYPE == PNG_ALIGN_OFFSET # if PNG_ALIGN_TYPE == PNG_ALIGN_OFFSET
# define png_alignof(type) offsetof(struct{char c; type t;}, t) # define png_alignof(type) offsetof(struct{char c; type t;}, t)
# else # else
# if PNG_ALIGN_TYPE == PNG_ALIGN_ALWAYS # if PNG_ALIGN_TYPE == PNG_ALIGN_ALWAYS
# define png_alignof(type) (1) # define png_alignof(type) 1
# endif # endif
/* Else leave png_alignof undefined to prevent use thereof */ /* Else leave png_alignof undefined to prevent use thereof */
# endif # endif
#endif #endif
/* This implicitly assumes alignment is always to a power of 2. */ /* This implicitly assumes alignment is always a multiple of 2. */
#ifdef png_alignof #ifdef png_alignof
# define png_isaligned(ptr, type)\ # define png_isaligned(ptr, type) \
(((type)((const char*)ptr-(const char*)0) & \ (((type)(size_t)((const void*)(ptr)) & (type)(png_alignof(type)-1)) == 0)
(type)(png_alignof(type)-1)) == 0)
#else #else
# define png_isaligned(ptr, type) 0 # define png_isaligned(ptr, type) 0
#endif #endif

View File

@ -1,7 +1,7 @@
/* pngrutil.c - utilities to read a PNG file /* pngrutil.c - utilities to read a PNG file
* *
* Copyright (c) 2018 Cosmin Truta * Copyright (c) 2018-2022 Cosmin Truta
* Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
* Copyright (c) 1996-1997 Andreas Dilger * Copyright (c) 1996-1997 Andreas Dilger
* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
@ -4618,14 +4618,13 @@ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
*/ */
{ {
png_bytep temp = png_ptr->big_row_buf + 32; png_bytep temp = png_ptr->big_row_buf + 32;
int extra = (int)((temp - (png_bytep)0) & 0x0f); size_t extra = (size_t)temp & 0x0f;
png_ptr->row_buf = temp - extra - 1/*filter byte*/; png_ptr->row_buf = temp - extra - 1/*filter byte*/;
temp = png_ptr->big_prev_row + 32; temp = png_ptr->big_prev_row + 32;
extra = (int)((temp - (png_bytep)0) & 0x0f); extra = (size_t)temp & 0x0f;
png_ptr->prev_row = temp - extra - 1/*filter byte*/; png_ptr->prev_row = temp - extra - 1/*filter byte*/;
} }
#else #else
/* Use 31 bytes of padding before and 17 bytes after row_buf. */ /* Use 31 bytes of padding before and 17 bytes after row_buf. */
png_ptr->row_buf = png_ptr->big_row_buf + 31; png_ptr->row_buf = png_ptr->big_row_buf + 31;