Make some string ops bounded and fix bug in editor toggle command (#54)

* Replace a few strcat/sprintf instances with bounded variants

Also cleaned up the whitespace in the parse_edit_action function as it was
not consistent.

Fix bug in editor format command introduced in earlier commit

* Fix bug in editor toggle command when an escaped @ is in the buffer

Previously, toggling between @ and \t would always try to convert
@ to \t, even if already toggled, iff an escaped @ was present in the
buffer (i.e. '@@').
This commit is contained in:
Kevin Fischer 2018-07-16 04:17:45 -05:00 committed by wyld-sw
parent 14855c273a
commit b27003e881
3 changed files with 151 additions and 141 deletions

View file

@ -2554,7 +2554,7 @@ ACMD(do_areas)
static void list_scanned_chars(struct char_data * list, struct char_data * ch, int static void list_scanned_chars(struct char_data * list, struct char_data * ch, int
distance, int door) distance, int door)
{ {
char buf[MAX_STRING_LENGTH], buf2[MAX_STRING_LENGTH]; char buf[MAX_STRING_LENGTH], buf2[MAX_STRING_LENGTH - 1];
const char *how_far[] = { const char *how_far[] = {
"close by", "close by",
@ -2589,16 +2589,16 @@ distance, int door)
if (!CAN_SEE(ch, i)) if (!CAN_SEE(ch, i))
continue; continue;
if (!*buf) if (!*buf)
sprintf(buf, "You see %s", GET_NAME(i)); snprintf(buf, sizeof(buf), "You see %s", GET_NAME(i));
else else
strcat(buf, GET_NAME(i)); strncat(buf, GET_NAME(i), sizeof(buf) - strlen(buf) - 1);
if (--count > 1) if (--count > 1)
strcat(buf, ", "); strncat(buf, ", ", sizeof(buf) - strlen(buf) - 1);
else if (count == 1) else if (count == 1)
strcat(buf, " and "); strncat(buf, " and ", sizeof(buf) - strlen(buf) - 1);
else { else {
sprintf(buf2, " %s %s.\r\n", how_far[distance], dirs[door]); snprintf(buf2, sizeof(buf2), " %s %s.\r\n", how_far[distance], dirs[door]);
strcat(buf, buf2); strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
} }
} }

View file

@ -470,14 +470,14 @@ static void script_syntax_highlighting(struct descriptor_data *d, char *string)
for (cmd = 0; *complete_cmd_info[cmd].command != '\n'; cmd++) { for (cmd = 0; *complete_cmd_info[cmd].command != '\n'; cmd++) {
if (complete_cmd_info[cmd].command_pointer == do_action) { if (complete_cmd_info[cmd].command_pointer == do_action) {
char replace_social[MAX_INPUT_LENGTH]; char replace_social[MAX_INPUT_LENGTH];
sprintf(replace_social, "\tc%s\tn", complete_cmd_info[cmd].command); snprintf(replace_social, MAX_INPUT_LENGTH, "\tc%s\tn", complete_cmd_info[cmd].command);
line = str_replace(line, complete_cmd_info[cmd].command, replace_social); line = str_replace(line, complete_cmd_info[cmd].command, replace_social);
} }
} }
} }
strcat(buffer, line); strncat(buffer, line, sizeof(buffer) - strlen(buffer) - 1);
strcat(buffer, "\tn\r\n"); strncat(buffer, "\tn\r\n", sizeof(buffer) - strlen(buffer) - 1);
} }
page_string(d, buffer, TRUE); page_string(d, buffer, TRUE);

View file

@ -102,7 +102,7 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
unsigned int total_len; unsigned int total_len;
char *s, *t, temp; char *s, *t, temp;
char buf[MAX_STRING_LENGTH]; char buf[MAX_STRING_LENGTH];
char buf2[MAX_STRING_LENGTH]; char buf2[MAX_STRING_LENGTH - 1];
switch (command) { switch (command) {
case PARSE_HELP: case PARSE_HELP:
@ -129,7 +129,16 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
write_to_output(d, "No string.\r\n"); write_to_output(d, "No string.\r\n");
break; break;
} }
if (strchr(*d->str, '@')) { bool has_at = FALSE;
for (char* c = *d->str; *c; ++c) {
if (*c == '@') {
if (*(++c) != '@') {
has_at = TRUE;
break;
}
}
}
if (has_at) {
parse_at(*d->str); parse_at(*d->str);
write_to_output(d, "Toggling (at) into (tab) Characters...\r\n"); write_to_output(d, "Toggling (at) into (tab) Characters...\r\n");
} else { } else {
@ -280,7 +289,7 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
} }
*buf = '\0'; *buf = '\0';
if (line_high < 999999 || line_low > 1) if (line_high < 999999 || line_low > 1)
sprintf(buf, "Current buffer range [%d - %d]:\r\n", line_low, line_high); snprintf(buf, sizeof(buf), "Current buffer range [%d - %d]:\r\n", line_low, line_high);
i = 1; i = 1;
total_len = 0; total_len = 0;
s = *d->str; s = *d->str;
@ -303,12 +312,12 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
if (s) { if (s) {
temp = *s; temp = *s;
*s = '\0'; *s = '\0';
strcat(buf, t); strncat(buf, t, sizeof(buf) - strlen(buf) - 1);
*s = temp; *s = temp;
} else } else
strcat(buf, t); strncat(buf, t, sizeof(buf) - strlen(buf) - 1);
/* This is kind of annoying...but some people like it. */ /* This is kind of annoying...but some people like it. */
sprintf(buf + strlen(buf), "\r\n%d line%sshown.\r\n", total_len, (total_len != 1) ? "s " : " "); snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "\r\n%d line%sshown.\r\n", total_len, (total_len != 1) ? "s " : " ");
page_string(d, buf, TRUE); page_string(d, buf, TRUE);
break; break;
case PARSE_LIST_NUM: case PARSE_LIST_NUM:
@ -358,20 +367,20 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
s++; s++;
temp = *s; temp = *s;
*s = '\0'; *s = '\0';
char buf3[8]; char buf3[9];
sprintf(buf3, "%4d: ", (i - 1)); sprintf(buf3, "%4d: ", (i - 1));
strcat(buf, buf3); strncat(buf, buf3, sizeof(buf) - strlen(buf) - 1);
strcat(buf, t); strncat(buf, t, sizeof(buf) - strlen(buf) - 1);
*s = temp; *s = temp;
t = s; t = s;
} }
if (s && t) { if (s && t) {
temp = *s; temp = *s;
*s = '\0'; *s = '\0';
strcat(buf, t); strncat(buf, t, sizeof(buf) - strlen(buf) - 1);
*s = temp; *s = temp;
} else if (t) } else if (t)
strcat(buf, t); strncat(buf, t, sizeof(buf) - strlen(buf) - 1);
page_string(d, buf, TRUE); page_string(d, buf, TRUE);
break; break;
@ -383,7 +392,7 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
return; return;
} }
line_low = atoi(buf); line_low = atoi(buf);
strcat(buf2, "\r\n"); strncat(buf2, "\r\n", sizeof(buf2) - strlen(buf2) - 1);
i = 1; i = 1;
*buf = '\0'; *buf = '\0';
@ -409,11 +418,11 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
return; return;
} }
if (*d->str && **d->str) if (*d->str && **d->str)
strcat(buf, *d->str); strncat(buf, *d->str, sizeof(buf) - strlen(buf) - 1);
*s = temp; *s = temp;
strcat(buf, buf2); strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
if (s && *s) if (s && *s)
strcat(buf, s); strncat(buf, s, sizeof(buf) - strlen(buf) - 1);
RECREATE(*d->str, char, strlen(buf) + 3); RECREATE(*d->str, char, strlen(buf) + 3);
strcpy(*d->str, buf); strcpy(*d->str, buf);
@ -431,7 +440,7 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
return; return;
} }
line_low = atoi(buf); line_low = atoi(buf);
strcat(buf2, "\r\n"); strncat(buf2, "\r\n", sizeof(buf2) - strlen(buf2) - 1);
i = 1; i = 1;
*buf = '\0'; *buf = '\0';
@ -458,18 +467,18 @@ void parse_edit_action(int command, char *string, struct descriptor_data *d)
temp = *s; temp = *s;
*s = '\0'; *s = '\0';
/* Put the first 'good' half of the text into storage. */ /* Put the first 'good' half of the text into storage. */
strcat(buf, *d->str); strncat(buf, *d->str, sizeof(buf) - strlen(buf) - 1);
*s = temp; *s = temp;
} }
/* Put the new 'good' line into place. */ /* Put the new 'good' line into place. */
strcat(buf, buf2); strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1);
if ((s = strchr(s, '\n')) != NULL) { if ((s = strchr(s, '\n')) != NULL) {
/* This means that we are at the END of the line, we want out of there, /* This means that we are at the END of the line, we want out of there,
* but we want s to point to the beginning of the line. AFTER the line * but we want s to point to the beginning of the line. AFTER the line
* we want edited. */ * we want edited. */
s++; s++;
/* Now put the last 'good' half of buffer into storage. */ /* Now put the last 'good' half of buffer into storage. */
strcat(buf, s); strncat(buf, s, sizeof(buf) - strlen(buf) - 1);
} }
/* Check for buffer overflow. */ /* Check for buffer overflow. */
if (strlen(buf) > d->max_str) { if (strlen(buf) > d->max_str) {
@ -511,7 +520,7 @@ int format_text(char **ptr_string, int mode, struct descriptor_data *d, unsigned
if ((flow = *ptr_string) == NULL) if ((flow = *ptr_string) == NULL)
return 0; return 0;
strcpy(str, flow); strncpy(str, flow, sizeof(str) - 1);
for (i = 0; i < low - 1; i++) { for (i = 0; i < low - 1; i++) {
start = strtok(str, "\n"); start = strtok(str, "\n");
@ -519,13 +528,13 @@ int format_text(char **ptr_string, int mode, struct descriptor_data *d, unsigned
write_to_output(d, "There aren't that many lines!\r\n"); write_to_output(d, "There aren't that many lines!\r\n");
return 0; return 0;
} }
strcat(formatted, strcat(start, "\n")); strncat(formatted, strcat(start, "\n"), sizeof(formatted) - strlen(formatted) - 1);
flow = strstr(flow, "\n"); flow = strstr(flow, "\n");
strcpy(str, ++flow); strncpy(str, ++flow, sizeof(str) - 1);
} }
if (IS_SET(mode, FORMAT_INDENT)) { if (IS_SET(mode, FORMAT_INDENT)) {
strcat(formatted, " "); strncat(formatted, " ", sizeof(formatted) - strlen(formatted) - 1);
line_chars = 3; line_chars = 3;
} else { } else {
line_chars = 0; line_chars = 0;
@ -591,14 +600,14 @@ int format_text(char **ptr_string, int mode, struct descriptor_data *d, unsigned
} }
if (line_chars + strlen(start) + 1 - color_chars > PAGE_WIDTH) { if (line_chars + strlen(start) + 1 - color_chars > PAGE_WIDTH) {
strcat(formatted, "\r\n"); strncat(formatted, "\r\n", sizeof(formatted) - strlen(formatted) - 1);
line_chars = 0; line_chars = 0;
color_chars = count_color_chars(start); color_chars = count_color_chars(start);
} }
if (!cap_next) { if (!cap_next) {
if (line_chars > 0) { if (line_chars > 0) {
strcat(formatted, " "); strncat(formatted, " ", sizeof(formatted) - strlen(formatted) - 1);
line_chars++; line_chars++;
} }
} else { } else {
@ -607,38 +616,38 @@ int format_text(char **ptr_string, int mode, struct descriptor_data *d, unsigned
} }
line_chars += strlen(start); line_chars += strlen(start);
strcat(formatted, start); strncat(formatted, start, sizeof(formatted) - strlen(formatted) - 1);
*flow = temp; *flow = temp;
} }
if (cap_next_next && *flow) { if (cap_next_next && *flow) {
if (line_chars + 3 - color_chars > PAGE_WIDTH) { if (line_chars + 3 - color_chars > PAGE_WIDTH) {
strcat(formatted, "\r\n"); strncat(formatted, "\r\n", sizeof(formatted) - strlen(formatted) - 1);
line_chars = 0; line_chars = 0;
color_chars = count_color_chars(start); color_chars = count_color_chars(start);
} else if (*flow == '\"' || *flow == '\'') { } else if (*flow == '\"' || *flow == '\'') {
char buf[MAX_STRING_LENGTH]; char buf[MAX_STRING_LENGTH - 1];
sprintf(buf, "%c ", *flow); snprintf(buf, sizeof(buf), "%c ", *flow);
strcat(formatted, buf); strncat(formatted, buf, sizeof(formatted) - strlen(formatted) - 1);
flow++; flow++;
line_chars++; line_chars++;
} else { } else {
strcat(formatted, " "); strncat(formatted, " ", sizeof(formatted) - strlen(formatted) - 1);
line_chars += 2; line_chars += 2;
} }
} }
} }
if (*flow) if (*flow)
strcat(formatted, "\r\n"); strncat(formatted, "\r\n", sizeof(formatted) - strlen(formatted) - 1);
strcat(formatted, flow); strncat(formatted, flow, sizeof(formatted) - strlen(formatted) - 1);
if (!*flow) if (!*flow)
strcat(formatted, "\r\n"); strncat(formatted, "\r\n", sizeof(formatted) - strlen(formatted) - 1);
if (strlen(formatted) + 1 > maxlen) int len = MIN(maxlen, strlen(formatted) + 1);
formatted[maxlen - 1] = '\0'; RECREATE(*ptr_string, char, len);
RECREATE(*ptr_string, char, MIN(maxlen, strlen(formatted) + 1)); strncpy(*ptr_string, formatted, len - 1);
strcpy(*ptr_string, formatted); (*ptr_string)[len - 1] = '\0';
return 1; return 1;
} }
@ -666,21 +675,22 @@ int replace_str(char **string, char *pattern, char *replacement, int rep_all, un
i = -1; i = -1;
break; break;
} }
strcat(replace_buffer, jetsam); strncat(replace_buffer, jetsam, max_size - strlen(replace_buffer) -1);
strcat(replace_buffer, replacement); strncat(replace_buffer, replacement, max_size - strlen(replace_buffer) - 1);
*flow = temp; *flow = temp;
flow += strlen(pattern); flow += strlen(pattern);
jetsam = flow; jetsam = flow;
} }
strcat(replace_buffer, jetsam); strncat(replace_buffer, jetsam, max_size - strlen(replace_buffer) - 1);
} else { } else {
if ((flow = (char *)strstr(*string, pattern)) != NULL) { if ((flow = (char *)strstr(*string, pattern)) != NULL) {
i++; i++;
flow += strlen(pattern); flow += strlen(pattern);
len = ((char *)flow - (char *)*string) - strlen(pattern); len = ((char *)flow - (char *)*string) - strlen(pattern);
strncpy(replace_buffer, *string, len); strncpy(replace_buffer, *string, len < max_size - 1 ? len : max_size - 1);
strcat(replace_buffer, replacement); replace_buffer[max_size - 1] = '\0';
strcat(replace_buffer, flow); strncat(replace_buffer, replacement, max_size - strlen(replace_buffer) - 1);
strncat(replace_buffer, flow, max_size - strlen(replace_buffer) - 1);
} }
} }