use arraysizeof/strsizeof macros for array/string sizes

master
an 2019-11-26 21:06:48 -05:00
parent a3995e795d
commit dd68032f7d
19 changed files with 54 additions and 62 deletions

View File

@ -132,7 +132,7 @@ void CFG_ReadCvarOverrides(const char **vars, int32_t num_vars)
for(i = 0; i < num_vars; i++)
{
q_strlcpy(&buff[1], vars[i], sizeof(buff) - 1);
q_strlcpy(&buff[1], vars[i], strsizeof(buff));
j = COM_CheckParm(buff);
if(j != 0 && j < com_argc - 1)
{

View File

@ -165,10 +165,10 @@ void Cbuf_Execute(void)
break;
}
if(i > (int32_t)sizeof(line) - 1)
if(i > (int32_t)strsizeof(line))
{
memcpy(line, text, sizeof(line) - 1);
line[sizeof(line) - 1] = 0;
memcpy(line, text, strsizeof(line));
line[strsizeof(line)] = 0;
}
else
{

View File

@ -844,7 +844,7 @@ const char *MSG_ReadString(void)
string[l] = c;
l++;
}
while(l < sizeof(string) - 1);
while(l < strsizeof(string));
string[l] = 0;

View File

@ -824,7 +824,7 @@ static const arg_completion_type_t arg_completion_types[] =
};
static const int32_t num_arg_completion_types =
sizeof(arg_completion_types) / sizeof(arg_completion_types[0]);
arraysizeof(arg_completion_types);
/*
============
@ -849,8 +849,8 @@ const char *FindCompletion(const char *partial, filelist_item_t *filelist, int32
if(init == 0)
{
init = 1;
strncpy(matched, file->name, sizeof(matched) - 1);
matched[sizeof(matched) - 1] = '\0';
strncpy(matched, file->name, strsizeof(matched));
matched[strsizeof(matched)] = '\0';
}
else
{

View File

@ -2023,7 +2023,7 @@ void Mod_LoadBrushModel(qmodel_t *mod, void *buffer)
// swap all the lumps
mod_base = (byte *)header;
for(i = 0; i < (int32_t) sizeof(dheader_t) / 4; i++)
for(i = 0; i < (int32_t)sizeof(dheader_t) / 4; i++)
((int32_t *)header)[i] = LittleLong(((int32_t *)header)[i]);
// load into heap

View File

@ -569,7 +569,7 @@ GLuint GL_CreateProgram(const GLchar *vertSource, const GLchar *fragSource, int3
}
else
{
if(gl_num_programs == (sizeof(gl_programs) / sizeof(GLuint)))
if(gl_num_programs == arraysizeof(gl_programs))
Host_Error("gl_programs overflow");
gl_programs[gl_num_programs] = program;

View File

@ -145,7 +145,7 @@ for a few moments
*/
void SCR_CenterPrint(const char *str) //update centerprint data
{
strncpy(scr_centerstring, str, sizeof(scr_centerstring) - 1);
strncpy(scr_centerstring, str, strsizeof(scr_centerstring));
scr_centertime_off = scr_centertime.value;
scr_centertime_start = cl.time;

View File

@ -1362,11 +1362,11 @@ void Host_Say(bool teamonly)
q_snprintf(text, sizeof(text), "\001<%s> %s", hostname.string, p);
// check length & truncate if necessary
j = (int32_t) strlen(text);
if(j >= (int32_t) sizeof(text) - 1)
j = strlen(text);
if(j >= (int32_t)strsizeof(text))
{
text[sizeof(text) - 2] = '\n';
text[sizeof(text) - 1] = '\0';
text[strsizeof(text) - 1] = '\n';
text[strsizeof(text)] = '\0';
}
else
{
@ -1440,11 +1440,11 @@ void Host_Tell_f(void)
q_snprintf(text, sizeof(text), "%s: %s", host_client->name, p);
// check length & truncate if necessary
j = (int32_t) strlen(text);
if(j >= (int32_t) sizeof(text) - 1)
j = strlen(text);
if(j >= (int32_t)strsizeof(text))
{
text[sizeof(text) - 2] = '\n';
text[sizeof(text) - 1] = '\0';
text[strsizeof(text) - 1] = '\n';
text[strsizeof(text)] = '\0';
}
else
{

View File

@ -999,7 +999,7 @@ void IN_SendKeyEvents(void)
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if(event.button.button < 1 ||
event.button.button > sizeof(buttonremap) / sizeof(buttonremap[0]))
event.button.button > arraysizeof(buttonremap))
{
Con_Printf("Ignored event for mouse button %" PRIi32 "\n",
event.button.button);

View File

@ -530,7 +530,7 @@ void Key_Message(int32_t key)
void Char_Message(int32_t key)
{
if(chat_bufferlen == sizeof(chat_buffer) - 1)
if(chat_bufferlen == strsizeof(chat_buffer))
return; // all full
chat_buffer[chat_bufferlen++] = key;

View File

@ -440,7 +440,7 @@ void M_ScanSaves(void)
continue;
fscanf(f, "%" PRIi32 "\n", &version);
fscanf(f, "%79s\n", name);
strncpy(m_filenames[i], name, sizeof(m_filenames[i]) - 1);
strncpy(m_filenames[i], name, strsizeof(m_filenames[i]));
// change _ back to space
for(j = 0; j < SAVEGAME_COMMENT_LENGTH; j++)
@ -1684,10 +1684,11 @@ bool M_Quit_TextEntry(void)
void M_Quit_Draw(void) //johnfitz -- modified for new quit message
{
char msg1[40];
char msg2[] = "by Ozkan, Ericw & Stevenaaus"; /* msg2/msg3 are mostly [40] */
char msg3[] = "Press y to quit";
int32_t boxlen;
static char const msg1[] = "QuakeSpasm " QUAKESPASM_VERSION;
static char const msg2[] = "by Ozkan, Ericw & Stevenaaus";
static char const msg3[] = "Press y to quit";
int32_t boxlen;
if(wasInMenus)
{
@ -1697,19 +1698,13 @@ void M_Quit_Draw(void) //johnfitz -- modified for new quit message
m_state = m_quit;
}
sprintf(msg1, "QuakeSpasm " QUAKESPASM_VERSION);
//okay, this is kind of fucked up. M_DrawTextBox will always act as if
//width is even. Also, the width and lines values are for the interior of the box,
//but the x and y values include the border.
boxlen = q_max(strlen(msg1), q_max((sizeof(msg2) - 1), (sizeof(msg3) - 1))) + 1;
boxlen = q_max(strsizeof(msg1), q_max(strsizeof(msg2), strsizeof(msg3))) + 1;
if(boxlen & 1) boxlen++;
M_DrawTextBox(160 - 4 * (boxlen + 2), 76, boxlen, 4);
//now do the text
M_Print(160 - 4 * strlen(msg1), 88, msg1);
M_Print(160 - 4 * (sizeof(msg2) - 1), 96, msg2);
M_PrintWhite(160 - 4 * (sizeof(msg3) - 1), 104, msg3);
M_Print(160 - 4 * strsizeof(msg1), 88, msg1);
M_Print(160 - 4 * strsizeof(msg2), 96, msg2);
M_PrintWhite(160 - 4 * strsizeof(msg3), 104, msg3);
}
//=============================================================================

View File

@ -68,7 +68,7 @@ static char *PF_VarString(int32_t first)
{
if(!dev_overflows.varstring || dev_overflows.varstring + CONSOLE_RESPAM_TIME < realtime)
{
Con_DWarning("PF_VarString: %" PRIi32 " characters exceeds standard limit of 255 (max = %" PRIi32 ").\n", (int32_t) s, (int32_t)(sizeof(out) - 1));
Con_DWarning("PF_VarString: %" PRIi32 " characters exceeds standard limit of 255 (max = %" PRIi32 ").\n", (int32_t)s, (int32_t)strsizeof(out));
dev_overflows.varstring = realtime;
}
}
@ -1766,5 +1766,5 @@ static builtin_t pr_builtin[] =
};
builtin_t *pr_builtins = pr_builtin;
int32_t pr_numbuiltins = sizeof(pr_builtin) / sizeof(pr_builtin[0]);
int32_t pr_numbuiltins = arraysizeof(pr_builtin);

View File

@ -43,7 +43,7 @@ int32_t pr_edict_size; // in bytes
uint16_t pr_crc;
int32_t type_size[8] =
int32_t type_size[8] =
{
1, // ev_void
1, // sizeof(string_t) / 4 // ev_string

View File

@ -144,7 +144,7 @@ static void PR_PrintStatement(dstatement_t *s)
{
int32_t i;
if((uint32_t)s->op < sizeof(pr_opnames) / sizeof(pr_opnames[0]))
if((uint32_t)s->op < arraysizeof(pr_opnames))
{
Con_Printf("%s ", pr_opnames[s->op]);
i = strlen(pr_opnames[s->op]);

View File

@ -29,27 +29,21 @@
#ifndef __QSTDINC_H
#define __QSTDINC_H
#include <sys/types.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdnoreturn.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/param.h>
#endif
#include <stdio.h>
#if defined(_MSC_VER) && (_MSC_VER < 1600)
#include "msinttypes/stdint.h"
#else
#include <stdint.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
_Static_assert(sizeof(float) == 4, "float not correct size");
_Static_assert(sizeof(int32_t) == 4, "int32_t not correct size");
@ -102,4 +96,7 @@ typedef vec_t vec5_t[5];
#define FUNCP_PRINTF(x,y)
#endif
#define arraysizeof(a) (sizeof(a) / sizeof(*a))
#define strsizeof(a) (sizeof(a) - 1)
#endif /* __QSTDINC_H */

View File

@ -197,7 +197,7 @@ void GLAlias_CreateShaders(void)
if(!gl_glsl_alias_able)
return;
r_alias_program = GL_CreateProgram(vertSource, fragSource, sizeof(bindings) / sizeof(bindings[0]), bindings);
r_alias_program = GL_CreateProgram(vertSource, fragSource, arraysizeof(bindings), bindings);
if(r_alias_program != 0)
{

View File

@ -877,7 +877,7 @@ void GLWorld_CreateShaders(void)
if(!gl_glsl_alias_able)
return;
r_world_program = GL_CreateProgram(vertSource, fragSource, sizeof(bindings) / sizeof(bindings[0]), bindings);
r_world_program = GL_CreateProgram(vertSource, fragSource, arraysizeof(bindings), bindings);
if(r_world_program != 0)
{

View File

@ -65,7 +65,7 @@ net_driver_t net_drivers[] =
}
};
const int32_t net_numdrivers = (sizeof(net_drivers) / sizeof(net_drivers[0]));
const int32_t net_numdrivers = arraysizeof(net_drivers);
#include "net_udp.h"
@ -96,5 +96,5 @@ net_landriver_t net_landrivers[] =
}
};
const int32_t net_numlandrivers = (sizeof(net_landrivers) / sizeof(net_landrivers[0]));
const int32_t net_numlandrivers = arraysizeof(net_landrivers);

View File

@ -65,7 +65,7 @@ net_driver_t net_drivers[] =
}
};
const int32_t net_numdrivers = (sizeof(net_drivers) / sizeof(net_drivers[0]));
const int32_t net_numdrivers = arraysizeof(net_drivers);
#include "net_wins.h"
@ -122,5 +122,5 @@ net_landriver_t net_landrivers[] =
}
};
const int32_t net_numlandrivers = (sizeof(net_landrivers) / sizeof(net_landrivers[0]));
const int32_t net_numlandrivers = arraysizeof(net_landrivers);