2
0
mirror of https://github.com/rehlds/rehlds.git synced 2025-01-15 08:08:12 +03:00

Fixed case when model name is too long

COM_LoadFile: Increase buffer room size
Mod_LoadModel: Use safe version COM_FileBase_s
Agree on the size of loadname[] and model_t::name[] buffers
This commit is contained in:
s1lentq 2022-04-10 03:32:19 +07:00
parent 2f0a402f9d
commit 4e0a3f312a
4 changed files with 66 additions and 25 deletions

View File

@ -1446,31 +1446,64 @@ char *COM_FileExtension(char *in)
#endif // #ifdef REHLDS_FIXES #endif // #ifdef REHLDS_FIXES
} }
// Fills "out" with the file name without path and extension. // Fills "out" with the file name without path and extension
void COM_FileBase(const char *in, char *out) void COM_FileBase(const char *in, char *out)
{ {
const char *start, *end; COM_FileBase_s(in, out, -1);
int len; }
*out = 0; // Extracts the base name of a file (no path, no extension, assumes '/' as path separator)
const char *COM_FileBase_s(const char *in, char *out, int size)
len = Q_strlen(in); {
if (len <= 0) if (!in || !in[0])
return;
start = in + len - 1;
end = in + len;
while (start >= in && *start != '/' && *start != '\\')
{ {
if (*start == '.') *out = '\0';
end = start; return NULL;
start--;
} }
start++;
len = end - start; int len = Q_strlen(in);
Q_strncpy(out, start, len); if (len <= 0)
out[len] = 0; return NULL;
// scan backward for '.'
int end = len - 1;
while (end && in[end] != '.' && !PATHSEPARATOR(in[end]))
end--;
// no '.', copy to end
if (in[end] != '.')
{
end = len - 1;
}
else
{
// Found ',', copy to left of '.'
end--;
}
// Scan backward for '/'
int start = len - 1;
while (start >= 0 && !PATHSEPARATOR(in[start]))
start--;
if (start < 0 || !PATHSEPARATOR(in[start]))
{
start = 0;
}
else
{
start++;
}
// Length of new sting
int maxcopy = end - start + 1;
if (size >= 0 && maxcopy >= size)
return NULL;
// Copy partial string
Q_strncpy(out, &in[start], maxcopy);
out[maxcopy] = '\0';
return out;
} }
void COM_DefaultExtension(char *path, char *extension) void COM_DefaultExtension(char *path, char *extension)
@ -1962,7 +1995,7 @@ int EXT_FUNC COM_FileSize(const char *filename)
unsigned char* EXT_FUNC COM_LoadFile(const char *path, int usehunk, int *pLength) unsigned char* EXT_FUNC COM_LoadFile(const char *path, int usehunk, int *pLength)
{ {
char base[33]; char base[MAX_PATH];
unsigned char *buf = NULL; unsigned char *buf = NULL;
#ifndef SWDS #ifndef SWDS
@ -1982,8 +2015,10 @@ unsigned char* EXT_FUNC COM_LoadFile(const char *path, int usehunk, int *pLength
} }
int len = FS_Size(hFile); int len = FS_Size(hFile);
COM_FileBase(path, base); if (!COM_FileBase_s(path, base, sizeof(base)))
base[32] = 0; Sys_Error("%s: Bad path length: %s", __func__, path);
base[32] = '\0';
switch (usehunk) switch (usehunk)
{ {

View File

@ -169,6 +169,7 @@ NOXREF char *COM_SkipPath(char *pathname);
void COM_StripExtension(char *in, char *out); void COM_StripExtension(char *in, char *out);
char *COM_FileExtension(char *in); char *COM_FileExtension(char *in);
void COM_FileBase(const char *in, char *out); void COM_FileBase(const char *in, char *out);
const char *COM_FileBase_s(const char *in, char *out, int size);
void COM_DefaultExtension(char *path, char *extension); void COM_DefaultExtension(char *path, char *extension);
void COM_UngetToken(void); void COM_UngetToken(void);
char *COM_Parse(char *data); char *COM_Parse(char *data);

View File

@ -29,7 +29,7 @@
#include "precompiled.h" #include "precompiled.h"
model_t *loadmodel; model_t *loadmodel;
char loadname[32]; char loadname[MAX_MODEL_NAME];
model_t mod_known[MAX_KNOWN_MODELS]; model_t mod_known[MAX_KNOWN_MODELS];
int mod_numknown; int mod_numknown;
unsigned char* mod_base; unsigned char* mod_base;
@ -330,7 +330,12 @@ model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean trackCRC)
Con_DPrintf("loading %s\n", mod->name); Con_DPrintf("loading %s\n", mod->name);
// allocate a new model // allocate a new model
COM_FileBase(mod->name, loadname); if (!COM_FileBase_s(mod->name, loadname, sizeof(loadname)))
{
Sys_Error("%s: Bad model name length: %s", __func__, mod->name);
return NULL;
}
loadmodel = mod; loadmodel = mod;
mod->needload = NL_PRESENT; mod->needload = NL_PRESENT;

View File

@ -46,7 +46,7 @@
#include "crc.h" #include "crc.h"
extern model_t* loadmodel; extern model_t* loadmodel;
extern char loadname[32]; extern char loadname[MAX_MODEL_NAME];
extern model_t mod_known[MAX_KNOWN_MODELS]; extern model_t mod_known[MAX_KNOWN_MODELS];
extern int mod_numknown; extern int mod_numknown;
extern unsigned char* mod_base; extern unsigned char* mod_base;