From d7871914acd2ed77f43344e36e08944524a67d9e Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Mon, 16 Jan 2017 00:10:38 +0100 Subject: Fix dangerous casts On Windows 64-bit, the size of long is 32-bit, thus any pointer to long cast is not valid. In any case, one should use intptr_t and ptrdiff_t when casting to integers. This commit attempts to fix all instances reported by GCC. When relevant, I replaced code by the macros PTR_ADD, ALIGN_UP from system.h Change-Id: I2273b0e8465d3c4689824717ed5afa5ed238a2dc --- apps/plugins/doom/d_deh.c | 4 ++-- apps/plugins/doom/d_main.c | 2 +- apps/plugins/doom/p_saveg.c | 30 +++++++++++++++--------------- apps/plugins/doom/z_zone.c | 4 ++-- apps/plugins/goban/util.c | 4 ++-- apps/plugins/lib/simple_viewer.c | 2 +- apps/plugins/lib/strncpy.c | 3 ++- apps/plugins/lrcplayer.c | 16 ++++++++-------- apps/plugins/mikmod/mikmod_internals.h | 2 +- apps/plugins/solitaire.c | 2 +- apps/plugins/zxbox/snapshot.c | 2 +- apps/plugins/zxbox/tapefile.c | 2 +- apps/plugins/zxbox/z80.c | 2 +- 13 files changed, 38 insertions(+), 37 deletions(-) (limited to 'apps/plugins') diff --git a/apps/plugins/doom/d_deh.c b/apps/plugins/doom/d_deh.c index abdaeae9ae..1a399e3b49 100644 --- a/apps/plugins/doom/d_deh.c +++ b/apps/plugins/doom/d_deh.c @@ -2082,7 +2082,7 @@ void deh_procSounds(DEHFILE *fpin, int fpout, char *line) S_sfx[indexnum].priority = (int)value; else if (!strcasecmp(key,deh_sfxinfo[3])) // Zero 1 - S_sfx[indexnum].link = (sfxinfo_t *)((long) value); + S_sfx[indexnum].link = (sfxinfo_t *)((intptr_t) value); else if (!strcasecmp(key,deh_sfxinfo[4])) // Zero 2 S_sfx[indexnum].pitch = (int)value; @@ -2091,7 +2091,7 @@ void deh_procSounds(DEHFILE *fpin, int fpout, char *line) S_sfx[indexnum].volume = (int)value; else if (!strcasecmp(key,deh_sfxinfo[6])) // Zero 4 - S_sfx[indexnum].data = (void *) ((long) value); // killough 5/3/98: changed cast + S_sfx[indexnum].data = (void *) ((intptr_t) value); // killough 5/3/98: changed cast else if (!strcasecmp(key,deh_sfxinfo[7])) // Neg. One 1 S_sfx[indexnum].usefulness = (int)value; diff --git a/apps/plugins/doom/d_main.c b/apps/plugins/doom/d_main.c index be45e6a37e..1afeffa502 100644 --- a/apps/plugins/doom/d_main.c +++ b/apps/plugins/doom/d_main.c @@ -807,7 +807,7 @@ void D_DoomMainSetup(void) // for statistics driver extern void* statcopy; - statcopy = (void*)(long)atoi(myargv[p+1]); + statcopy = (void*)(intptr_t)atoi(myargv[p+1]); printf ("External statistics registered.\n"); } diff --git a/apps/plugins/doom/p_saveg.c b/apps/plugins/doom/p_saveg.c index a704bfa83c..891b3b0503 100644 --- a/apps/plugins/doom/p_saveg.c +++ b/apps/plugins/doom/p_saveg.c @@ -44,7 +44,7 @@ byte *save_p; // Pads save_p to a 4-byte boundary // so that the load/save works on SGI&Gecko. -#define PADSAVEP() do { save_p += (4 - ((unsigned long) save_p & 3)) & 3; } while (0) +#define PADSAVEP() do { save_p += (4 - ((intptr_t) save_p & 3)) & 3; } while (0) // // P_ArchivePlayers // @@ -95,7 +95,7 @@ void P_UnArchivePlayers (void) for (j=0 ; jnext) if (th->function == P_MobjThinker) - th->prev = (thinker_t *)(long)(++number_of_thinkers); + th->prev = (thinker_t *)(intptr_t)(++number_of_thinkers); } // phares 9/13/98: Moved this code outside of P_ArchiveThinkers so the @@ -448,10 +448,10 @@ void P_UnArchiveThinkers (void) PADSAVEP(); memcpy (mobj, save_p, sizeof(mobj_t)); save_p += sizeof(mobj_t); - mobj->state = states + (unsigned long) mobj->state; + mobj->state = states + (intptr_t) mobj->state; if (mobj->player) - (mobj->player = &players[(unsigned long) mobj->player - 1]) -> mo = mobj; + (mobj->player = &players[(intptr_t) mobj->player - 1]) -> mo = mobj; P_SetThingPosition (mobj); mobj->info = &mobjinfo[mobj->type]; @@ -770,7 +770,7 @@ void P_UnArchiveSpecials (void) ceiling_t *ceiling = Z_Malloc (sizeof(*ceiling), PU_LEVEL, NULL); memcpy (ceiling, save_p, sizeof(*ceiling)); save_p += sizeof(*ceiling); - ceiling->sector = §ors[(unsigned long)ceiling->sector]; + ceiling->sector = §ors[(intptr_t)ceiling->sector]; ceiling->sector->ceilingdata = ceiling; //jff 2/22/98 if (ceiling->thinker.function) @@ -787,10 +787,10 @@ void P_UnArchiveSpecials (void) vldoor_t *door = Z_Malloc (sizeof(*door), PU_LEVEL, NULL); memcpy (door, save_p, sizeof(*door)); save_p += sizeof(*door); - door->sector = §ors[(unsigned long)door->sector]; + door->sector = §ors[(intptr_t)door->sector]; //jff 1/31/98 unarchive line remembered by door as well - door->line = (long)door->line!=-1? &lines[(unsigned long)door->line] : NULL; + door->line = (intptr_t)door->line!=-1? &lines[(intptr_t)door->line] : NULL; door->sector->ceilingdata = door; //jff 2/22/98 door->thinker.function = T_VerticalDoor; @@ -804,7 +804,7 @@ void P_UnArchiveSpecials (void) floormove_t *floor = Z_Malloc (sizeof(*floor), PU_LEVEL, NULL); memcpy (floor, save_p, sizeof(*floor)); save_p += sizeof(*floor); - floor->sector = §ors[(unsigned long)floor->sector]; + floor->sector = §ors[(intptr_t)floor->sector]; floor->sector->floordata = floor; //jff 2/22/98 floor->thinker.function = T_MoveFloor; P_AddThinker (&floor->thinker); @@ -817,7 +817,7 @@ void P_UnArchiveSpecials (void) plat_t *plat = Z_Malloc (sizeof(*plat), PU_LEVEL, NULL); memcpy (plat, save_p, sizeof(*plat)); save_p += sizeof(*plat); - plat->sector = §ors[(unsigned long)plat->sector]; + plat->sector = §ors[(intptr_t)plat->sector]; plat->sector->floordata = plat; //jff 2/22/98 if (plat->thinker.function) @@ -834,7 +834,7 @@ void P_UnArchiveSpecials (void) lightflash_t *flash = Z_Malloc (sizeof(*flash), PU_LEVEL, NULL); memcpy (flash, save_p, sizeof(*flash)); save_p += sizeof(*flash); - flash->sector = §ors[(unsigned long)flash->sector]; + flash->sector = §ors[(intptr_t)flash->sector]; flash->thinker.function = T_LightFlash; P_AddThinker (&flash->thinker); break; @@ -846,7 +846,7 @@ void P_UnArchiveSpecials (void) strobe_t *strobe = Z_Malloc (sizeof(*strobe), PU_LEVEL, NULL); memcpy (strobe, save_p, sizeof(*strobe)); save_p += sizeof(*strobe); - strobe->sector = §ors[(unsigned long)strobe->sector]; + strobe->sector = §ors[(intptr_t)strobe->sector]; strobe->thinker.function = T_StrobeFlash; P_AddThinker (&strobe->thinker); break; @@ -858,7 +858,7 @@ void P_UnArchiveSpecials (void) glow_t *glow = Z_Malloc (sizeof(*glow), PU_LEVEL, NULL); memcpy (glow, save_p, sizeof(*glow)); save_p += sizeof(*glow); - glow->sector = §ors[(unsigned long)glow->sector]; + glow->sector = §ors[(intptr_t)glow->sector]; glow->thinker.function = T_Glow; P_AddThinker (&glow->thinker); break; @@ -870,7 +870,7 @@ void P_UnArchiveSpecials (void) fireflicker_t *flicker = Z_Malloc (sizeof(*flicker), PU_LEVEL, NULL); memcpy (flicker, save_p, sizeof(*flicker)); save_p += sizeof(*flicker); - flicker->sector = §ors[(unsigned long)flicker->sector]; + flicker->sector = §ors[(intptr_t)flicker->sector]; flicker->thinker.function = T_FireFlicker; P_AddThinker (&flicker->thinker); break; @@ -883,7 +883,7 @@ void P_UnArchiveSpecials (void) elevator_t *elevator = Z_Malloc (sizeof(*elevator), PU_LEVEL, NULL); memcpy (elevator, save_p, sizeof(*elevator)); save_p += sizeof(*elevator); - elevator->sector = §ors[(unsigned long)elevator->sector]; + elevator->sector = §ors[(intptr_t)elevator->sector]; elevator->sector->floordata = elevator; //jff 2/22/98 elevator->sector->ceilingdata = elevator; //jff 2/22/98 elevator->thinker.function = T_MoveElevator; diff --git a/apps/plugins/doom/z_zone.c b/apps/plugins/doom/z_zone.c index df00d6a9d7..d2b6111b60 100644 --- a/apps/plugins/doom/z_zone.c +++ b/apps/plugins/doom/z_zone.c @@ -252,8 +252,8 @@ void Z_Init(void) // Align on cache boundary - zone = (memblock_t *) ((unsigned long)zonebase + CACHE_ALIGN - - ((unsigned long)zonebase & (CACHE_ALIGN-1))); + zone = (memblock_t *) ((intptr_t)zonebase + CACHE_ALIGN - + ((intptr_t)zonebase & (CACHE_ALIGN-1))); rover = zone; // Rover points to base of zone mem zone->next = zone->prev = zone; // Single node diff --git a/apps/plugins/goban/util.c b/apps/plugins/goban/util.c index bb78316346..206606a4bb 100644 --- a/apps/plugins/goban/util.c +++ b/apps/plugins/goban/util.c @@ -73,7 +73,7 @@ void metadata_summary (void) void * align_buffer (void *buffer, size_t * buffer_size) { - unsigned int wasted = (-(long) buffer) & 3; + unsigned int wasted = (-(intptr_t) buffer) & 3; if (!buffer || !buffer_size) { @@ -88,7 +88,7 @@ align_buffer (void *buffer, size_t * buffer_size) *buffer_size -= wasted; - return (void *) (((char *) buffer) + wasted); + return PTR_ADD(buffer, wasted); } diff --git a/apps/plugins/lib/simple_viewer.c b/apps/plugins/lib/simple_viewer.c index 06cc9c1a71..6927d84a96 100644 --- a/apps/plugins/lib/simple_viewer.c +++ b/apps/plugins/lib/simple_viewer.c @@ -69,7 +69,7 @@ static const char* get_next_line(const char *text, struct view_info *info) w = 1; #else unsigned short ch; - n = ((long)rb->utf8decode(ptr, &ch) - (long)ptr); + n = ((intptr_t)rb->utf8decode(ptr, &ch) - (intptr_t)ptr); if (rb->is_diacritic(ch, NULL)) w = 0; else diff --git a/apps/plugins/lib/strncpy.c b/apps/plugins/lib/strncpy.c index 1b552ee15c..9ad3594287 100644 --- a/apps/plugins/lib/strncpy.c +++ b/apps/plugins/lib/strncpy.c @@ -41,13 +41,14 @@ QUICKREF #include #include "plugin.h" #include "_ansi.h" +#include /*SUPPRESS 560*/ /*SUPPRESS 530*/ /* Nonzero if either X or Y is not aligned on a "long" boundary. */ #define ROCKBOX_UNALIGNED(X, Y) \ - (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1))) + (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1))) #if LONG_MAX == 2147483647L #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) diff --git a/apps/plugins/lrcplayer.c b/apps/plugins/lrcplayer.c index 392e78e77f..c7f36968a1 100644 --- a/apps/plugins/lrcplayer.c +++ b/apps/plugins/lrcplayer.c @@ -545,7 +545,7 @@ static struct lrc_brpos *calc_brpos(struct lrc_line *lrc_line, int i) c = rb->utf8seek(cr.str, 1); w = 1; #else - c = ((long)rb->utf8decode(cr.str, &ch) - (long)cr.str); + c = ((intptr_t)rb->utf8decode(cr.str, &ch) - (intptr_t)cr.str); if (rb->is_diacritic(ch, NULL)) w = 0; else @@ -885,7 +885,7 @@ static bool parse_lrc_line(char *line, off_t file_offset) lrc_line->time_start = (time/10)*10; lrc_line->old_time_start = lrc_line->time_start; add_lrc_line(lrc_line, NULL); - file_offset += (long)tagend - (long)str; + file_offset += (intptr_t)tagend - (intptr_t)str; str = tagend; } if (!first_lrc_line) @@ -908,7 +908,7 @@ static bool parse_lrc_line(char *line, off_t file_offset) if (!tagend) break; *tagend = 0; time = get_time_value(tagstart+1, false, - file_offset + ((long)tagstart - (long)str)); + file_offset + ((intptr_t)tagstart - (intptr_t)str)); *tagend++ = '>'; if (time < 0) { @@ -923,7 +923,7 @@ static bool parse_lrc_line(char *line, off_t file_offset) return false; nword++; } - file_offset += (long)tagend - (long)str; + file_offset += (intptr_t)tagend - (intptr_t)str; tagstart = str = tagend; time_start = time; } @@ -1159,7 +1159,7 @@ static int unsynchronize(char* tag, int len, bool *ff_found) } } if(ff_found) *ff_found = _ff_found; - return (long)wp - (long)tag; + return (intptr_t)wp - (intptr_t)tag; } static int read_unsynched(int fd, void *buf, int len, bool *ff_found) @@ -1471,7 +1471,7 @@ static void parse_id3v2(int fd) utf_decode = rb->utf16BEdecode; } } - bytesread -= (long)p - (long)tag; + bytesread -= (intptr_t)p - (intptr_t)tag; tag = p; while ( bytesread > 0 @@ -1529,7 +1529,7 @@ static void parse_id3v2(int fd) lrc_line->old_time_start = -1; if(is_crlf) p += chsiz; } - bytesread -= (long)p - (long)tag; + bytesread -= (intptr_t)p - (intptr_t)tag; tag = p; if(!add_lrc_line(lrc_line, utf8line)) break; @@ -2922,7 +2922,7 @@ enum plugin_status plugin_start(const void* parameter) #endif lrc_buffer = rb->plugin_get_buffer(&lrc_buffer_size); - lrc_buffer = (void *)(((long)lrc_buffer+3)&~3); /* 4 bytes aligned */ + lrc_buffer = ALIGN_UP(lrc_buffer, 4); /* 4 bytes aligned */ lrc_buffer_size = (lrc_buffer_size - 4)&~3; reset_current_data(); diff --git a/apps/plugins/mikmod/mikmod_internals.h b/apps/plugins/mikmod/mikmod_internals.h index acd514ad39..78d7b52045 100644 --- a/apps/plugins/mikmod/mikmod_internals.h +++ b/apps/plugins/mikmod/mikmod_internals.h @@ -703,7 +703,7 @@ extern int Voice_Stopped_internal(SBYTE); /*========== SIMD mixing helper functions =============*/ -#define IS_ALIGNED_16(ptr) (!(((int)(ptr)) & 15)) +#define IS_ALIGNED_16(ptr) (!(((intptr_t)(ptr)) & 15)) /* Altivec helper function */ #if defined HAVE_ALTIVEC diff --git a/apps/plugins/solitaire.c b/apps/plugins/solitaire.c index b5e982d208..b476fa11b4 100644 --- a/apps/plugins/solitaire.c +++ b/apps/plugins/solitaire.c @@ -1547,7 +1547,7 @@ static int load_game( void ) return -1; retval = 0; /* Assume good case */ - if( ( rb->lseek( fd, -sizeof( int ), SEEK_END ) == -((ssize_t)sizeof( int ))-1 ) + if( ( rb->lseek( fd, -(off_t)sizeof( int ), SEEK_END ) == -((ssize_t)sizeof( int ))-1 ) || ( rb->read( fd, &checksum, sizeof( int ) ) < ((ssize_t)sizeof( int )) ) || ( rb->lseek( fd, 0, SEEK_SET ) == -1 ) || save_read( fd, &cur_card, sizeof( int ), &checksum ) diff --git a/apps/plugins/zxbox/snapshot.c b/apps/plugins/zxbox/snapshot.c index 468f46667e..74845bac8e 100644 --- a/apps/plugins/zxbox/snapshot.c +++ b/apps/plugins/zxbox/snapshot.c @@ -362,7 +362,7 @@ static void read_compressed_data(SNFILE *fp, byte *start, unsigned size, GET_DATA(ch); if(p + times > end) { put_msg("Warning: Repeat parameter too large in snapshot"); - times = (int) ((long) end - (long) p); + times = (int) ((intptr_t) end - (intptr_t) p); } for(j = 0; j < times; j++) *p++ = ch; } diff --git a/apps/plugins/zxbox/tapefile.c b/apps/plugins/zxbox/tapefile.c index b030f8dab9..054cae1aba 100644 --- a/apps/plugins/zxbox/tapefile.c +++ b/apps/plugins/zxbox/tapefile.c @@ -216,7 +216,7 @@ static struct tzxblock tzxb[NUMBLOCKID] = { }; -#define PTRDIFF(pe, ps) ((int) (((long) (pe) - (long) (ps)) / sizeof(*pe))) +#define PTRDIFF(pe, ps) pe - ps static char tzxheader[] = {'Z','X','T','a','p','e','!',0x1A}; diff --git a/apps/plugins/zxbox/z80.c b/apps/plugins/zxbox/z80.c index f6f1de7468..683584f471 100644 --- a/apps/plugins/zxbox/z80.c +++ b/apps/plugins/zxbox/z80.c @@ -48,7 +48,7 @@ static byte *a64kmalloc(int num64ksegs) /*exit(1);*/ } - return (byte *) (( (long) bigmem & ~((long) 0xFFFF)) + 0x10000); + return (byte *) (( (intptr_t) bigmem & ~((intptr_t) 0xFFFF)) + 0x10000); } -- cgit v1.2.3