Added bounds check to strcat.

This commit is contained in:
Leonardo Zide
2025-12-15 11:19:23 -03:00
parent a08dede88b
commit c2db2d8d45
6 changed files with 50 additions and 11 deletions
+2 -2
View File
@@ -1668,7 +1668,7 @@ bool lcPiecesLibrary::LoadPrimitive(lcLibraryPrimitive* Primitive)
{
char Name[LC_PIECE_NAME_LEN];
lcstrcpy(Name, "8/");
strcat(Name, Primitive->mName);
lcstrcat(Name, Primitive->mName);
lcstrupr(Name);
LowPrimitive = FindPrimitive(Name); // todo: low primitives don't work with studlogo, because the low stud gets added as shared
@@ -1770,7 +1770,7 @@ void lcPiecesLibrary::GetCategoryEntries(const char* CategoryKeywords, bool Grou
char ParentName[LC_PIECE_NAME_LEN];
lcstrcpy(ParentName, Info->mFileName);
*strchr(ParentName, 'P') = '\0';
strcat(ParentName, ".dat");
lcstrcat(ParentName, ".dat");
Parent = FindPiece(ParentName, nullptr, false, false);
+1 -1
View File
@@ -850,7 +850,7 @@ bool lcModel::LoadBinary(lcFile* file)
file->ReadFloats(rot.GetFloats(), 3);
file->ReadU8(&color, 1);
file->ReadBuffer(name, 9);
strcat(name, ".dat");
lcstrcat(name, ".dat");
file->ReadU8(&step, 1);
file->ReadU8(&group, 1);
+36 -5
View File
@@ -30,12 +30,12 @@ char* lcstrupr(char* string)
return string;
}
size_t lcstrcpy(char* dest, const char* source, size_t count)
size_t lcstrcpy(char* dest, const char* source, size_t size)
{
if (count == 0)
if (size == 0)
return 0;
for (size_t i = 0; i < count; i++)
for (size_t i = 0; i < size; i++)
{
dest[i] = source[i];
@@ -45,7 +45,38 @@ size_t lcstrcpy(char* dest, const char* source, size_t count)
}
}
dest[--count] = '\0';
dest[--size] = '\0';
return count;
return size;
}
size_t lcstrcat(char* dest, const char* source, size_t size)
{
char* d = dest;
const char* s = source;
size_t n = size;
size_t dlen;
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dest;
n = size - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0')
{
if (n != 1)
{
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return (dlen + (s - source));
}
+8 -1
View File
@@ -2,10 +2,17 @@
char* lcstrcasestr(const char* s, const char* find);
char* lcstrupr(char* string);
size_t lcstrcpy(char* dest, const char* source, size_t count);
size_t lcstrcpy(char* dest, const char* source, size_t size);
size_t lcstrcat(char* dest, const char* source, size_t size);
template<size_t size>
size_t lcstrcpy(char (&dest)[size], const char* source)
{
return lcstrcpy(dest, source, size);
}
template<size_t size>
size_t lcstrcat(char (&dest)[size], const char* source)
{
return lcstrcat(dest, source, size);
}
+2 -1
View File
@@ -15,6 +15,7 @@
#include "lc_qutils.h"
#include "lc_synth.h"
#include "lc_traintrack.h"
#include "lc_string.h"
constexpr float LC_PIECE_CONTROL_POINT_SIZE = 10.0f;
@@ -336,7 +337,7 @@ bool lcPiece::FileLoad(lcFile& file)
}
else
file.ReadBuffer(name, LC_PIECE_NAME_LEN);
strcat(name, ".dat");
lcstrcat(name, ".dat");
PieceInfo* pInfo = lcGetPiecesLibrary()->FindPiece(name, nullptr, true, false);
SetPieceInfo(pInfo, QString(), true, true);
+1 -1
View File
@@ -2239,7 +2239,7 @@ std::pair<bool, QString> Project::ExportPOVRay(const QString& FileName)
if (sscanf(Line,"%128s%128s%10s", Src, Dst, Flags) != 3)
continue;
strcat(Src, ".dat");
lcstrcat(Src, ".dat");
PieceInfo* Info = Library->FindPiece(Src, nullptr, false, false);
if (!Info)