From 1cf4f7795aef99b81f6d4afe80b6d9d32bbcedbe Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Wed, 17 May 2017 09:22:38 -0700 Subject: [PATCH] IBM1130: more robust detection of SCP command files dropped into GUI as discussed in #456 --- Ibm1130/ibm1130_gui.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Ibm1130/ibm1130_gui.c b/Ibm1130/ibm1130_gui.c index d73d8a03..7a9268a1 100644 --- a/Ibm1130/ibm1130_gui.c +++ b/Ibm1130/ibm1130_gui.c @@ -1438,19 +1438,30 @@ static BOOL is_scp_file(const char *filename) FILE *f = fopen(filename, "r"); int lines = 0, comment_lines = 0; BOOL result = TRUE; + size_t i; if (!f) return FALSE; - while (1) { + while (result) { CONST char *cptr; cptr = fgets(cbuf, sizeof(cbuf), f); if (cptr == NULL) break; + if (strlen(cptr) == sizeof(cbuf)-1) /* VERY long lines are not SCP commands */ + result = FALSE; + if (!strchr(cptr, '\n')) /* lines without newlines are not SCP commands */ + result = FALSE; + if (!memcmp(cptr,"!// ", 4)) /* indirect deck file literals are not SCP commands */ + result = FALSE; cptr = sim_trim_endspc(cbuf); while (sim_isspace (*cptr)) /* trim leading space */ cptr++; ++lines; + for (i = 0; i < strlen(cptr); i++) + if ((cptr[i] & 0x80) || + ((!isprint(cptr[i])) && (!isspace(cptr[i])))) + result = FALSE; /* SCP files only have printable ASCII */ if ((*cptr == ';') || (*cptr == '#')) { /* ignore comments */ ++comment_lines; continue;