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

@ -577,8 +577,8 @@
/* This is used because in some compiler implementations non-aligned
* structure members are supported, so the offsetof approach below fails.
* 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
* and understand it.
* is good for performance. Do not do this unless you have tested the
* result and understand it.
*/
# define png_alignof(type) (sizeof(type))
#else
@ -586,17 +586,16 @@
# define png_alignof(type) offsetof(struct{char c; type t;}, t)
# else
# if PNG_ALIGN_TYPE == PNG_ALIGN_ALWAYS
# define png_alignof(type) (1)
# define png_alignof(type) 1
# endif
/* Else leave png_alignof undefined to prevent use thereof */
# 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
# define png_isaligned(ptr, type) \
(((type)((const char*)ptr-(const char*)0) & \
(type)(png_alignof(type)-1)) == 0)
(((type)(size_t)((const void*)(ptr)) & (type)(png_alignof(type)-1)) == 0)
#else
# define png_isaligned(ptr, type) 0
#endif

View File

@ -1,7 +1,7 @@
/* 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) 1996-1997 Andreas Dilger
* 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;
int extra = (int)((temp - (png_bytep)0) & 0x0f);
size_t extra = (size_t)temp & 0x0f;
png_ptr->row_buf = temp - extra - 1/*filter byte*/;
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*/;
}
#else
/* Use 31 bytes of padding before and 17 bytes after row_buf. */
png_ptr->row_buf = png_ptr->big_row_buf + 31;