Skip to content
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

Fix buffer overflow in process_option() #492

Open
wants to merge 2 commits 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
40 changes: 18 additions & 22 deletions src/host/premake.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,33 +258,29 @@ int process_arguments(lua_State* L, int argc, const char** argv)
*/
int process_option(lua_State* L, const char* arg)
{
char key[512];
const char* value;

/* If a value is specified, split the option into a key/value pair */
char* ptr = strchr(arg, '=');
if (ptr)
const char* value = strchr(arg, '=');
if (value)
{
int len = (int)(ptr - arg);
if (len > 511) len = 511;
strncpy(key, arg, len);
key[len] = '\0';
value = ptr + 1;
}
else
{
strcpy(key, arg);
value = "";
}
static const char key[] = "scripts=";
size_t len = sizeof(key) - 1;

/* Store it in the Options table, which is already on the stack */
lua_pushstring(L, value);
lua_setfield(L, -3, key);
/* Store it in the Options table, which is already on the stack */
lua_pushlstring(L, arg, value - arg);
lua_pushstring(L, ++value);
lua_settable(L, -4);

/* The /scripts option gets picked up here to find the built-in scripts */
if (strcmp(key, "scripts") == 0 && strlen(value) > 0)
/* The /scripts option gets picked up here to find the built-in scripts */
if (value - arg == len && !memcmp(key, arg, len) && value[0])
{
scripts_path = value;
}
}
else
{
scripts_path = value;
/* No value, store empty string in the Options table */
lua_pushliteral(L, "");
lua_setfield(L, -3, arg);
}

return OKAY;
Expand Down