Skip to content

Avoid overrunning the 256 icon memory buffer #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/raygui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,7 @@ static unsigned int guiIcons[RAYGUI_ICON_MAX_ICONS*RAYGUI_ICON_DATA_ELEMENTS] =

// NOTE: A pointer to current icons array should be defined
static unsigned int *guiIconsPtr = guiIcons;
static int guiIconsCount = RAYGUI_ICON_MAX_ICONS;

#endif // !RAYGUI_NO_ICONS && !RAYGUI_CUSTOM_ICONS

Expand Down Expand Up @@ -4609,7 +4610,7 @@ char **GuiLoadIcons(const char *fileName, bool loadIconsName)
{
if (loadIconsName)
{
guiIconsName = (char **)RAYGUI_MALLOC(iconCount*sizeof(char **));
guiIconsName = (char **)RAYGUI_MALLOC(iconCount*sizeof(char *));
for (int i = 0; i < iconCount; i++)
{
guiIconsName[i] = (char *)RAYGUI_MALLOC(RAYGUI_ICON_MAX_NAME_LENGTH);
Expand All @@ -4618,8 +4619,13 @@ char **GuiLoadIcons(const char *fileName, bool loadIconsName)
}
else fseek(rgiFile, iconCount*RAYGUI_ICON_MAX_NAME_LENGTH, SEEK_CUR);

if (iconCount > RAYGUI_ICON_MAX_ICONS)
{
iconCount = RAYGUI_ICON_MAX_ICONS;
}
// Read icons data directly over internal icons array
fread(guiIconsPtr, sizeof(unsigned int), (int)iconCount*((int)iconSize*(int)iconSize/32), rgiFile);
guiIconsCount = iconCount;
}

fclose(rgiFile);
Expand Down Expand Up @@ -4671,6 +4677,7 @@ char **GuiLoadIconsFromMemory(const unsigned char *fileData, int dataSize, bool

int iconDataSize = iconCount*((int)iconSize*(int)iconSize/32)*(int)sizeof(unsigned int);
guiIconsPtr = (unsigned int *)RAYGUI_MALLOC(iconDataSize);
guiIconsCount = iconCount;

memcpy(guiIconsPtr, fileDataPtr, iconDataSize);
}
Expand Down Expand Up @@ -5048,11 +5055,15 @@ static const char *GetTextIcon(const char *text, int *iconId)

if (text[pos] == '#')
{
*iconId = TextToInteger(iconValue);
int rawIconId = TextToInteger(iconValue);
if (rawIconId < guiIconsCount)
{
*iconId = rawIconId;

// Move text pointer after icon
// WARNING: If only icon provided, it could point to EOL character: '\0'
if (*iconId >= 0) text += (pos + 1);
// Move text pointer after icon
// WARNING: If only icon provided, it could point to EOL character: '\0'
if (*iconId >= 0) text += (pos + 1);
}
}
}
#endif
Expand Down