IBM1130: Cleanup potential buffer overruns in SCA device

This commit is contained in:
Mark Pizzolato 2019-07-16 04:33:34 -07:00
parent 437e5a4bb5
commit d3228e85b0

View file

@ -312,7 +312,8 @@ static void sca_socket_error (void)
free(sca_unit.filename);
if (sca_unit.flags & UNIT_LISTEN) {
sprintf(name, "(Listening on port %s)", sca_port);
name[sizeof (name) - 1] = '\0';
snprintf(name, sizeof (name) - 1, "(Listening on port %s)", sca_port);
sca_unit.filename = mstring(name);
printf("%s\n", name);
}
@ -463,9 +464,9 @@ static t_stat sca_attach (UNIT *uptr, CONST char *cptr)
if (sim_parse_addr (cptr, host, sizeof(host), NULL, port, sizeof(port), SCA_DEFAULT_PORT, NULL))
return SCPE_ARG;
if ((0 == strcmp(port, cptr)) && (0 == strcmp(port, "dummy")))
strcpy(port, SCA_DEFAULT_PORT);
strlcpy(port, SCA_DEFAULT_PORT, sizeof (port));
sprintf(sca_port, "%s%s%s:%s", strchr(host, ':') ? "[" : "", host, strchr(host, ':') ? "]" : "", port);
snprintf(sca_port, sizeof (sca_port) - 1, "%s%s%s:%s", strchr(host, ':') ? "[" : "", host, strchr(host, ':') ? "]" : "", port);
/* else if nondigits specified, ignore... but the command has to have something there otherwise the core scp */
/* attach_cmd() routine complains "too few arguments". */
@ -478,7 +479,8 @@ static t_stat sca_attach (UNIT *uptr, CONST char *cptr)
SETBIT(sca_unit.flags, UNIT_LISTEN); /* note that we are listening, not yet connected */
sprintf(name, "(Listening on port %s)", sca_port);
name[sizeof (name) - 1] = '\0';
snprintf(name, sizeof (name) - 1, "(Listening on port %s)", sca_port);
sca_unit.filename = mstring(name);
printf("%s\n", sca_unit.filename);
@ -493,11 +495,11 @@ static t_stat sca_attach (UNIT *uptr, CONST char *cptr)
if (sim_parse_addr (cptr, host, sizeof(host), NULL, port, sizeof(port), SCA_DEFAULT_PORT, NULL))
return SCPE_ARG;
if ((0 == strcmp(cptr, port)) && (0 == strcmp(host, ""))) {
strcpy(host, port);
strcpy(port, SCA_DEFAULT_PORT);
strlcpy(host, port, sizeof (host));
strlcpy(port, SCA_DEFAULT_PORT, sizeof (port));
}
sprintf(sca_port, "%s%s%s:%s", strchr(host, ':') ? "[" : "", host, strchr(host, ':') ? "]" : "", port);
snprintf(sca_port, sizeof (sca_port) - 1, "%s%s%s:%s", strchr(host, ':') ? "[" : "", host, strchr(host, ':') ? "]" : "", port);
if ((sca_sock = sim_connect_sock(sca_port, NULL, NULL)) == INVALID_SOCKET)
return SCPE_OPENERR;
@ -511,7 +513,8 @@ static t_stat sca_attach (UNIT *uptr, CONST char *cptr)
sim_os_ms_sleep(1000);
if (1 == sim_check_conn(sca_sock, 0)) { /* sca_sock appears in "writable" set -- connect completed */
sprintf(name, "%s%s%s:%s", strchr(host, ':') ? "[" : "", host, strchr(host, ':') ? "]" : "", port);
name[sizeof (name) - 1] = '\0';
snprintf(name, sizeof (name) - 1, "%s%s%s:%s", strchr(host, ':') ? "[" : "", host, strchr(host, ':') ? "]" : "", port);
sca_unit.filename = mstring(name);
SETBIT(sca_dsw, SCA_DSW_READY);
}