Convert loops using unsigned integers as iterators to use regular signed ints instead

https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html

Doing this as cleanly as possible involved demoting several function parameters concerning object sizes and counts from size_t to int--I'm fine with this change as the functions in question are not actually capable of handling input with sizes larger than what can be stored in a signed 32-bit integer, making the use of size_t here misleading at best.

git-svn-id: https://svn.eduke32.com/eduke32@7673 1a8010ca-5511-0410-912e-c29ae57300e0

# Conflicts:
#	source/build/src/polymost.cpp
#	source/build/src/texcache.cpp
#	source/build/src/tilepacker.cpp
This commit is contained in:
terminx 2019-05-19 03:56:13 +00:00 committed by Christoph Oelckers
parent 79752ee168
commit b0a4b6a1ee
20 changed files with 73 additions and 79 deletions

View file

@ -547,21 +547,21 @@ int32_t MV_PlayFLAC(char *ptr, uint32_t length, int32_t loopstart, int32_t loope
const size_t field = value - entry;
value += 1;
for (size_t t = 0; t < loopStartTagCount && vc_loopstart == NULL; ++t)
for (int t = 0; t < loopStartTagCount && vc_loopstart == NULL; ++t)
{
char const * const tag = loopStartTags[t];
if (field == strlen(tag) && Bstrncasecmp(entry, tag, field) == 0)
vc_loopstart = Xstrdup(value);
}
for (size_t t = 0; t < loopEndTagCount && vc_loopend == NULL; ++t)
for (int t = 0; t < loopEndTagCount && vc_loopend == NULL; ++t)
{
char const * const tag = loopEndTags[t];
if (field == strlen(tag) && Bstrncasecmp(entry, tag, field) == 0)
vc_loopend = Xstrdup(value);
}
for (size_t t = 0; t < loopLengthTagCount && vc_looplength == NULL; ++t)
for (int t = 0; t < loopLengthTagCount && vc_looplength == NULL; ++t)
{
char const * const tag = loopLengthTags[t];
if (field == strlen(tag) && Bstrncasecmp(entry, tag, field) == 0)

View file

@ -78,21 +78,21 @@ static void MV_GetVorbisCommentLoops(VoiceNode *voice, vorbis_comment *vc)
const size_t field = value - entry;
value += 1;
for (size_t t = 0; t < loopStartTagCount && vc_loopstart == NULL; ++t)
for (int t = 0; t < loopStartTagCount && vc_loopstart == NULL; ++t)
{
auto tag = loopStartTags[t];
if (field == Bstrlen(tag) && Bstrncasecmp(entry, tag, field) == 0)
vc_loopstart = value;
}
for (size_t t = 0; t < loopEndTagCount && vc_loopend == NULL; ++t)
for (int t = 0; t < loopEndTagCount && vc_loopend == NULL; ++t)
{
auto tag = loopEndTags[t];
if (field == Bstrlen(tag) && Bstrncasecmp(entry, tag, field) == 0)
vc_loopend = value;
}
for (size_t t = 0; t < loopLengthTagCount && vc_looplength == NULL; ++t)
for (int t = 0; t < loopLengthTagCount && vc_looplength == NULL; ++t)
{
auto tag = loopLengthTags[t];
if (field == Bstrlen(tag) && Bstrncasecmp(entry, tag, field) == 0)
@ -141,14 +141,14 @@ static size_t read_vorbis(void *ptr, size_t size, size_t nmemb, void *datasource
if (vorb->length == vorb->pos)
return 0;
size_t nread = 0;
int32_t nread = 0;
for (; nmemb > 0; nmemb--, nread++)
{
size_t bytes = vorb->length - vorb->pos;
int32_t bytes = vorb->length - vorb->pos;
if (size < bytes)
bytes = size;
if ((signed)size < bytes)
bytes = (int32_t)size;
memcpy(ptr, (uint8_t *)vorb->ptr + vorb->pos, bytes);
vorb->pos += bytes;

View file

@ -149,17 +149,13 @@ typedef struct _CACHE1D_FIND_REC {
} CACHE1D_FIND_REC;
int32_t klistaddentry(CACHE1D_FIND_REC **rec, const char *name, int32_t type, int32_t source);
void klistfree(CACHE1D_FIND_REC *rec);
CACHE1D_FIND_REC *klistpath(const char *path, const char *mask, int32_t type);
CACHE1D_FIND_REC *klistpath(const char *path, const char *mask, int type);
extern int32_t lz4CompressionLevel;
int32_t kdfread(void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_kfd fil);
int32_t kdfread_LZ4(void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_kfd fil);
#if 0
int32_t dfread(void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_FILE fil);
void kdfwrite(const void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_kfd fil);
#endif
void dfwrite(const void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_FILE fil);
void dfwrite_LZ4(const void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_FILE fil);
int32_t kdfread(void *buffer, int dasizeof, int count, buildvfs_kfd fil);
int32_t kdfread_LZ4(void *buffer, int dasizeof, int count, buildvfs_kfd fil);
void dfwrite(const void *buffer, int dasizeof, int count, buildvfs_FILE fil);
void dfwrite_LZ4(const void *buffer, int dasizeof, int count, buildvfs_FILE fil);
#ifdef __cplusplus
}

View file

@ -15,7 +15,7 @@ extern uint32_t crc32table[8][256];
extern uint32_t crc32table[4][256];
#endif
extern uint32_t Bcrc32(const void* data, size_t length, uint32_t crc);
extern uint32_t Bcrc32(const void* data, int length, uint32_t crc);
extern void initcrc32table(void);
#ifdef __cplusplus

View file

@ -20,8 +20,8 @@ extern "C" {
typedef int32_t (*klzw_readfunc)(intptr_t, void *, int32_t);
typedef void (*klzw_writefunc)(intptr_t, void const *, int32_t);
int32_t klzw_read_compressed(void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f, klzw_readfunc readfunc);
void klzw_write_compressed(const void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f, klzw_writefunc writefunc);
int32_t klzw_read_compressed(void *buffer, int dasizeof, int count, intptr_t const f, klzw_readfunc readfunc);
void klzw_write_compressed(const void * const buffer, int dasizeof, int count, intptr_t const f, klzw_writefunc writefunc);
#ifdef __cplusplus
}

View file

@ -176,7 +176,7 @@ typedef struct
uint32_t flags; // controls initialization, etc
osdcvar_t *cvars;
uint32_t numcvars;
int32_t numcvars;
osdsymbol_t *symbols;
osdsymbol_t *symbptrs[OSDMAXSYMBOLS];

View file

@ -34,6 +34,6 @@ typedef struct
void png_set_pal(uint8_t const * data, int numentries);
void png_set_text(char const * keyword, char const * text);
void png_write(buildvfs_FILE file, uint32_t width, uint32_t height, uint8_t type, uint8_t const * data);
void png_write(buildvfs_FILE const file, int const width, int const height, uint8_t const type, uint8_t const * const data);
#endif

View file

@ -99,11 +99,11 @@ enable_if_t<is_integral<T>::value, size_t> buildprintpiece(binwrap<T> x)
{
make_unsigned_t<T> const data = x.data;
size_t constexpr numChars = sizeof(x)*CHAR_BIT;
int constexpr numChars = sizeof(x)*CHAR_BIT;
char str[numChars+1];
str[numChars] = '\0';
for (size_t p = 0; p < numChars; ++p)
for (int p = 0; p < numChars; ++p)
str[numChars - 1 - p] = '0' + (char)((data >> p) & 1);
initputs(str);
@ -115,11 +115,11 @@ enable_if_t<is_integral<T>::value, size_t> buildprintpiece(octwrap<T> x)
{
make_unsigned_t<T> const data = x.data;
size_t constexpr numChars = (sizeof(x)*CHAR_BIT + 2) / 3;
int constexpr numChars = (sizeof(x)*CHAR_BIT + 2) / 3;
char str[numChars+1];
str[numChars] = '\0';
for (size_t p = 0; p < numChars; ++p)
for (int p = 0; p < numChars; ++p)
str[numChars - 1 - p] = '0' + (char)((data >> (p*3)) & 7);
initputs(str);
@ -133,12 +133,12 @@ enable_if_t<is_integral<T>::value, size_t> buildprintpiece(hexwrap<T> x)
make_unsigned_t<T> const data = x.data;
size_t constexpr numChars = (sizeof(x)*CHAR_BIT + 3) / 4;
int constexpr numChars = (sizeof(x)*CHAR_BIT + 3) >> 2;
char str[numChars+1];
str[numChars] = '\0';
for (size_t p = 0; p < numChars; ++p)
str[numChars - 1 - p] = hexletters[(size_t)((data >> (p*4)) & 0xF)];
for (int p = 0; p < numChars; ++p)
str[numChars - 1 - p] = hexletters[(int)((data >> (p<<2)) & 0xF)];
initputs(str);
return numChars;
@ -151,12 +151,12 @@ enable_if_t<is_integral<T>::value, size_t> buildprintpiece(HEXwrap<T> x)
make_unsigned_t<T> const data = x.data;
size_t constexpr numChars = (sizeof(x)*CHAR_BIT + 3) / 4;
int constexpr numChars = (sizeof(x)*CHAR_BIT + 3) >> 2;
char str[numChars+1];
str[numChars] = '\0';
for (size_t p = 0; p < numChars; ++p)
str[numChars - 1 - p] = HEXletters[(size_t)((data >> (p*4)) & 0xF)];
for (int p = 0; p < numChars; ++p)
str[numChars - 1 - p] = HEXletters[(int)((data >> (p<<2)) & 0xF)];
initputs(str);
return numChars;

View file

@ -817,7 +817,7 @@ int initgroupfile(const char *filename)
}
temp2 = 0;
for (uint8_t i=0;i<3;i++)
for (int i=0;i<3;i++)
{
// get the string length
kread_grp(numgroupfiles, &temp, 1);
@ -1430,7 +1430,7 @@ CACHE1D_FIND_REC *klistpath(const char *_path, const char *mask, int32_t type)
if ((name[0] == '.' && name[1] == 0) ||
(name[0] == '.' && name[1] == '.' && name[2] == 0))
continue;
bool const isdir = buildvfs_isdir(name);
if ((type & CACHE1D_FIND_DIR) && !isdir) continue;
if ((type & CACHE1D_FIND_FILE) && isdir) continue;
@ -1657,7 +1657,7 @@ static void dfwrite_func(intptr_t fp, const void *inbuf, int32_t length)
}
int32_t kdfread(void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_kfd fil)
int32_t kdfread(void *buffer, int dasizeof, int count, buildvfs_kfd fil)
{
return klzw_read_compressed(buffer, dasizeof, count, (intptr_t)fil, kdfread_func);
}
@ -1669,7 +1669,7 @@ int32_t kdfread(void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_kfd fil)
static char compressedDataStackBuf[131072];
int32_t lz4CompressionLevel = LZ4_COMPRESSION_ACCELERATION_VALUE;
int32_t kdfread_LZ4(void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_kfd fil)
int32_t kdfread_LZ4(void *buffer, int dasizeof, int count, buildvfs_kfd fil)
{
int32_t leng;
@ -1696,21 +1696,21 @@ int32_t kdfread_LZ4(void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_kfd
}
void dfwrite(const void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_FILE fil)
void dfwrite(const void *buffer, int dasizeof, int count, buildvfs_FILE fil)
{
klzw_write_compressed(buffer, dasizeof, count, (intptr_t)fil, dfwrite_func);
}
void dfwrite_LZ4(const void *buffer, bsize_t dasizeof, bsize_t count, buildvfs_FILE fil)
void dfwrite_LZ4(const void *buffer, int dasizeof, int count, buildvfs_FILE fil)
{
char * pCompressedData = compressedDataStackBuf;
int32_t const maxCompressedSize = LZ4_compressBound(dasizeof * count);
char * pCompressedData = compressedDataStackBuf;
int const maxCompressedSize = LZ4_compressBound(dasizeof * count);
if (maxCompressedSize > ARRAY_SSIZE(compressedDataStackBuf))
pCompressedData = (char *)Xaligned_alloc(16, maxCompressedSize);
int32_t const leng = LZ4_compress_fast((const char*) buffer, pCompressedData, dasizeof*count, maxCompressedSize, lz4CompressionLevel);
int32_t const swleng = B_LITTLE32(leng);
int const leng = LZ4_compress_fast((const char*) buffer, pCompressedData, dasizeof*count, maxCompressedSize, lz4CompressionLevel);
int const swleng = B_LITTLE32(leng);
buildvfs_fwrite(&swleng, sizeof(swleng), 1, fil);
buildvfs_fwrite(pCompressedData, leng, 1, fil);

View file

@ -3,7 +3,7 @@
#include "compat.h"
#include "crc32.h"
uint32_t Bcrc32(const void* data, size_t length, uint32_t crc)
uint32_t Bcrc32(const void* data, int length, uint32_t crc)
{
const uint32_t* current = (const uint32_t*) data;
uint8_t const * currentChar;

View file

@ -213,10 +213,10 @@ struct decompress_info
{
klzw_readfunc readfunc;
intptr_t f;
uint32_t kgoal;
int32_t kgoal;
};
static uint32_t decompress_part(struct decompress_info * x)
static int decompress_part(struct decompress_info * x)
{
intptr_t const f = x->f;
auto readfunc = x->readfunc;
@ -236,7 +236,7 @@ static uint32_t decompress_part(struct decompress_info * x)
}
// Read from 'f' into 'buffer'.
int32_t klzw_read_compressed(void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f, klzw_readfunc readfunc)
int32_t klzw_read_compressed(void *buffer, int dasizeof, int count, intptr_t const f, klzw_readfunc readfunc)
{
char *ptr = (char *)buffer;
@ -255,9 +255,7 @@ int32_t klzw_read_compressed(void *buffer, bsize_t dasizeof, bsize_t count, intp
Bmemcpy(ptr, lzwrawbuf, (int32_t)dasizeof);
uint32_t k = (int32_t)dasizeof;
for (uint32_t i=1; i<count; i++)
for (int i=1, k=dasizeof; i<count; i++)
{
if (k >= x.kgoal)
{
@ -265,7 +263,7 @@ int32_t klzw_read_compressed(void *buffer, bsize_t dasizeof, bsize_t count, intp
if (k) return -1;
}
uint32_t j = 0;
int j = 0;
if (dasizeof >= 4)
{
@ -295,7 +293,7 @@ struct compress_info
{
klzw_writefunc writefunc;
intptr_t f;
uint32_t k;
int32_t k;
};
static void compress_part(struct compress_info * x)
@ -313,7 +311,7 @@ static void compress_part(struct compress_info * x)
}
// Write from 'buffer' to 'f'.
void klzw_write_compressed(const void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f, klzw_writefunc writefunc)
void klzw_write_compressed(const void * const buffer, int dasizeof, int count, intptr_t const f, klzw_writefunc writefunc)
{
char const *ptr = (char const *)buffer;
@ -332,9 +330,9 @@ void klzw_write_compressed(const void *buffer, bsize_t dasizeof, bsize_t count,
if ((x.k = dasizeof) > LZWSIZE-dasizeof)
compress_part(&x);
for (uint32_t i=1; i<count; i++)
for (int i=1; i<count; i++)
{
uint32_t j = 0;
int j = 0;
if (dasizeof >= 4)
{

View file

@ -528,7 +528,7 @@ static int osdfunc_listsymbols(osdcmdptr_t parm)
int const var = hash_find(&h_cvars, symb->name);
if ((unsigned)var < osd->numcvars && OSD_CvarModified(&osd->cvars[var]))
if ((unsigned)var < OSDMAXSYMBOLS && OSD_CvarModified(&osd->cvars[var]))
{
OSD_Printf("%s*", osd->draw.highlight);
OSD_Printf("%-*s", maxwidth-1, symb->name);
@ -2203,7 +2203,7 @@ void OSD_WriteCvars(buildvfs_FILE fp)
char buf[64];
for (unsigned i = 0; i < osd->numcvars; i++)
for (int i = 0; i < osd->numcvars; i++)
{
osdcvardata_t const &pData = *osd->cvars[i].pData;

View file

@ -59,14 +59,14 @@ void png_set_text(char const * const keyword, char const * const text)
Bmemcpy(png.text + keylen + 1, text, textlen);
}
void png_write(buildvfs_FILE const file, uint32_t const width, uint32_t const height,
void png_write(buildvfs_FILE const file, int const width, int const height,
uint8_t const type, uint8_t const * const data)
{
png.file = file;
png_write_buf("\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8);
png_ihdr_t const png_header = { B_BIG32(width), B_BIG32(height), 8, type, 0 };
png_ihdr_t const png_header = { B_BIG32((unsigned)width), B_BIG32((unsigned)height), 8, type, 0 };
png_write_chunk(sizeof(png_ihdr_t), "IHDR", (uint8_t const *)&png_header, 0);
if (png.text)
@ -75,8 +75,8 @@ void png_write(buildvfs_FILE const file, uint32_t const width, uint32_t const he
DO_FREE_AND_NULL(png.text);
}
uint32_t const bytesPerPixel = (type == PNG_TRUECOLOR ? 3 : 1);
uint32_t const bytesPerLine = width * bytesPerPixel;
int const bytesPerPixel = (type == PNG_TRUECOLOR ? 3 : 1);
int const bytesPerLine = width * bytesPerPixel;
if (png.pal_data)
{
@ -84,10 +84,10 @@ void png_write(buildvfs_FILE const file, uint32_t const width, uint32_t const he
DO_FREE_AND_NULL(png.pal_data);
}
unsigned const linesiz = height * bytesPerLine + height;
int const linesiz = height * bytesPerLine + height;
uint8_t *lines = (uint8_t *) Xcalloc(1, linesiz);
for (unative_t i = 0; i < height; i++)
for (int i = 0; i < height; i++)
Bmemcpy(lines + i * bytesPerLine + i + 1, data + i * bytesPerLine, bytesPerLine);
png_write_chunk(linesiz, "IDAT", lines, CHUNK_COMPRESSED);

View file

@ -77,7 +77,7 @@ int videoCaptureScreen(const char *filename, char inverseit)
if (inverseit)
{
for (bsize_t i=0, j = ydim * bytesPerLine; i<j; i+=3)
for (int i=0, j = ydim * bytesPerLine; i<j; i+=3)
swapchar(&imgBuf[i], &imgBuf[i+2]);
}

View file

@ -1632,7 +1632,7 @@ ACTOR_STATIC void G_MoveFallers(void)
if ((sector[sectNum].floorz-pSprite->z) < ZOFFSET2)
{
#ifndef EDUKE32_STANDALONE
for (size_t x = 0, x_end = 1+(krand()&7); x < x_end; ++x)
for (int x = 0, x_end = 1+(krand()&7); x < x_end; ++x)
RANDOMSCRAP(pSprite, spriteNum);
#endif
DELETE_SPRITE_AND_CONTINUE(spriteNum);
@ -2136,7 +2136,7 @@ crack_default:
if (A_IncurDamage(spriteNum) < 0)
goto next_sprite;
for (bsize_t k=0; k<16; k++)
for (int k=0; k<16; k++)
{
j = A_InsertSprite(SECT(spriteNum), SX(spriteNum), SY(spriteNum), SZ(spriteNum) - (krand() % (48 << 8)),
SCRAP3 + (krand() & 3), -8, 48, 48, krand() & 2047, (krand() & 63) + 64,
@ -2265,7 +2265,7 @@ DETONATE:
#ifndef EDUKE32_STANDALONE
if (pSprite->xrepeat)
for (bsize_t x=0; x<8; x++)
for (int x=0; x<8; x++)
RANDOMSCRAP(pSprite, spriteNum);
#endif
@ -5240,7 +5240,7 @@ ACTOR_STATIC void G_MoveMisc(void) // STATNUM 5
pSprite->yrepeat = forceRepeat;
pSprite->shade = (forceRepeat >> 1) - 48;
for (bsize_t j = pData[0]; j > 0; j--)
for (int j = pData[0]; j > 0; j--)
A_SetSprite(spriteNum, CLIPMASK0);
goto next_sprite;
}
@ -6602,7 +6602,7 @@ ACTOR_STATIC void G_MoveEffectors(void) //STATNUM 3
walltype *pWall = &wall[sector[sectNum].wallptr];
for (bsize_t l=sector[sectNum].wallnum; l>0; l--, pWall++)
for (int l=sector[sectNum].wallnum; l>0; l--, pWall++)
{
if (pWall->hitag == 1)
continue;
@ -8109,7 +8109,7 @@ static void A_DoLight(int spriteNum)
return;
#ifndef EDUKE32_STANDALONE
for (bsize_t ii=0; ii<2; ii++)
for (int ii=0; ii<2; ii++)
{
if (pSprite->picnum <= 0) // oob safety
break;

View file

@ -207,8 +207,8 @@ void Anim_Init(void)
if (anm.numsounds)
{
anim->sounds = (animsound_t *)Xmalloc(anm.numsounds * sizeof(animsound_t));
size_t const numsounds = anm.numsounds;
for (size_t i = 0; i < numsounds; ++i)
int const numsounds = anm.numsounds;
for (int i = 0; i < numsounds; ++i)
{
defaultanmsound const & src = anm.sounds[i];
animsound_t & dst = anim->sounds[i];

View file

@ -7047,7 +7047,7 @@ void A_SpawnWallGlass(int spriteNum, int wallNum, int glassCnt)
int16_t sect = -1;
for (bsize_t j = glassCnt; j > 0; --j)
for (int j = glassCnt; j > 0; --j)
{
v1.x += v.x;
v1.y += v.y;
@ -7088,7 +7088,7 @@ void A_SpawnCeilingGlass(int spriteNum, int sectNum, int glassCnt)
vec2_t v = { tabledivide32_noinline(wall[wallNum + 1].x - v1.x, glassCnt + 1),
tabledivide32_noinline(wall[wallNum + 1].y - v1.y, glassCnt + 1) };
for (bsize_t j = glassCnt; j > 0; j--)
for (int j = glassCnt; j > 0; j--)
{
v1.x += v.x;
v1.y += v.y;
@ -7117,7 +7117,7 @@ void A_SpawnRandomGlass(int spriteNum, int wallNum, int glassCnt)
tabledivide32_noinline(wall[wall[wallNum].point2].y - wall[wallNum].y, glassCnt + 1) };
int16_t sectNum = sprite[spriteNum].sectnum;
for (bsize_t j = glassCnt; j > 0; j--)
for (int j = glassCnt; j > 0; j--)
{
v1.x += v.x;
v1.y += v.y;

View file

@ -2281,7 +2281,7 @@ static void Net_ReceiveServerUpdate(ENetEvent *event)
ticrandomseed = serverupdate.seed;
for (uint32_t playerIndex = 0; playerIndex < serverupdate.numplayers; ++playerIndex)
for (int playerIndex = 0; playerIndex < serverupdate.numplayers; ++playerIndex)
{
Bmemcpy(&playerupdate, updatebuf, sizeof(serverplayerupdate_t));
updatebuf += sizeof(serverplayerupdate_t);

View file

@ -1112,7 +1112,7 @@ void G_PrintGameQuotes(int32_t snum)
if (k > 1 && !reserved_quote)
y += k <= 8 ? (height * (k-1))>>3 : height;
for (size_t i = MAXUSERQUOTES-1; i < MAXUSERQUOTES; --i)
for (int i = 0; i < MAXUSERQUOTES; i++)
{
k = user_quote_time[i];

View file

@ -1039,7 +1039,7 @@ void G_OperateActivators(int lotag, int playerNum)
sector[pCycler[0]].ceilingshade = pCycler[3];
walltype *pWall = &wall[sector[pCycler[0]].wallptr];
for (bsize_t j = sector[pCycler[0]].wallnum; j > 0; j--, pWall++)
for (int j = sector[pCycler[0]].wallnum; j > 0; j--, pWall++)
pWall->shade = pCycler[3];
}
}