Skip to content

quote CMDLINE env var exe name if needed, fix memory leak in expandEnvVars, memory corruption in set command #104

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

Merged
merged 10 commits into from
Dec 22, 2024
Merged
2 changes: 1 addition & 1 deletion cmd/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int cmd_set(char *param)
error_out_of_memory();
return E_NoMem;
}
len = dos_read(0, promptBuf, promptBuffer);
len = dos_read(0, promptBuf, promptBuffer-1);
if(cbreak || len < 0) {
free(promptBuf);
return E_CBreak;
Expand Down
22 changes: 16 additions & 6 deletions shell/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,15 @@ void execute(char *first, char *rest, int lh_lf)
dprintf(("[EXEC: %s %s]\n", fullname, rest));

if(strlen(rest) > MAX_EXTERNAL_COMMAND_SIZE) {
char *fullcommandline = malloc( strlen( first ) + strlen( rest ) + 2 );
char *fullcommandline = malloc( strlen( first ) + strlen( rest ) + 3 );
if( fullcommandline == NULL ) return;
sprintf( fullcommandline, "%s%s", first, rest );
if ( strchr( first, ' ' ) ) {
sprintf( fullcommandline, "\"%s\"%s", first, rest );

}
else {
sprintf( fullcommandline, "%s%s", first, rest );
}
if( chgEnv( LONG_CMDLINE_ENV_NAME, fullcommandline ) != 0 ) {
free( fullcommandline );
return;
Expand Down Expand Up @@ -686,8 +692,10 @@ int expandEnvVars(char *ip, char * const line)
*tp = '\0';

if((evar = getEnv(ip)) != 0) {
if(cp >= parsedMax(strlen(evar)))
return 0;
if(cp >= parsedMax(strlen(evar))) {
free(evar);
return 0;
}
cp = stpcpy(cp, evar);
free(evar);
} else if(matchtok(ip, "ERRORLEVEL")) {
Expand All @@ -698,8 +706,10 @@ int expandEnvVars(char *ip, char * const line)
if(0 == (evar = cwd(0))) {
return 0;
} else {
if(cp >= parsedMax(strlen(evar)))
return 0;
if(cp >= parsedMax(strlen(evar))) {
free(evar);
return 0;
}
cp = stpcpy(cp, evar);
free(evar);
}
Expand Down