Skip to content

Commit

Permalink
better compatibility when command line is greater than 126
Browse files Browse the repository at this point in the history
from RBIL and other sources, the command line should end in just \r and if greater than 126 chars, the size byte should be 127 not actual size; still keep \0 if command line is short enough but allow 126 chars without terminating \0, set size to 127 if >126
  • Loading branch information
PerditionC committed Jun 4, 2021
1 parent 26c999c commit ba97fbc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 4 additions & 3 deletions include/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
#include <fmemory.h>
#include "../include/misc.h"

#define MAX_EXTERNAL_COMMAND_SIZE 125
#define MAX_EXTERNAL_COMMAND_SIZE 126
/* The maximal external command line is:
128: overall space for the command line)
- 1: Pascal string length byte
- 1: '\r' (at the end)
- 1: '\0' (at the very end)
= 125
= 126
Note: prior versions of FreeCOM also had a '\0' after the '\r',
but per RBIL & other documents, the command line is only terminated by a '\r' (0Dh value)
*/

enum InternalErrorCodes {
Expand Down
22 changes: 18 additions & 4 deletions lib/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,37 @@ int exec(const char *cmd, char *cmdLine, const unsigned segOfEnv)
# define buf dosCMDTAIL
# define memcpy _fmemcpy
#else
unsigned char buf[128];
unsigned char buf[MAX_EXTERNAL_COMMAND_SIZE+2]; /* 128 bytes is max size in PSP, 2 bytes for size and terminator */
#endif
struct fcb fcb1,
fcb2;
struct ExecBlock execBlock;
int retval;
int cmdLen;


assert(cmd);
assert(cmdLine);
assert(strlen(cmdLine) <= 125);

invalidateNLSbuf();

/* generate Pascal string from the command line */
memcpy(&buf[1], cmdLine, buf[0] = strlen(cmdLine));
memcpy(&buf[1] + buf[0], "\xd", 2);
/* we assume passed in c string (ASCIIZ) */
cmdLen = strlen(cmdLine);
if (cmdLen > MAX_EXTERNAL_COMMAND_SIZE) {
/* we assume CMDLINE environment variable already set with full cmdLine */
/* so we simply truncate passed command line to max size, terminated by \r, no \0 for callee */
/* for maximum compatibility set size to 127 (0x7f) */
buf[0] = (unsigned char)(MAX_EXTERNAL_COMMAND_SIZE+1);
memcpy(&buf[1], cmdLine, MAX_EXTERNAL_COMMAND_SIZE);
/* terminate with just carriage return \r */
memcpy(&buf[1] + buf[0], "\xd", 1);
} else {
/* set size of actual command line and copy to buffer */
memcpy(&buf[1], cmdLine, buf[0] = strlen(cmdLine));
/* ensure terminated with \r\0 */
memcpy(&buf[1] + buf[0], "\xd", 2);
}

/* fill FCBs */
if ((cmdLine = parsfnm(cmdLine, &fcb1, 1)) != 0)
Expand Down

0 comments on commit ba97fbc

Please sign in to comment.