fix all cases of strcpy on overlapping strings.

This commit is contained in:
Denis Vlasenko 2008-07-22 20:16:55 +00:00
parent 68a192c007
commit 0f293b96dc
7 changed files with 15 additions and 5 deletions

View File

@ -289,7 +289,7 @@ static char **print_formatted(char *f, char **argv)
/* Remove size modifiers - "%Ld" would try to printf /* Remove size modifiers - "%Ld" would try to printf
* long long, we pass long, and it spews garbage */ * long long, we pass long, and it spews garbage */
if ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') { if ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
strcpy(f, f + 1); overlapping_strcpy(f, f + 1);
} }
//FIXME: actually, the same happens with bare "%d": //FIXME: actually, the same happens with bare "%d":
//it printfs an int, but we pass long! //it printfs an int, but we pass long!

View File

@ -1219,7 +1219,7 @@ static void add_cmd_block(char *cmdstr)
slashes++; slashes++;
/* Odd number of preceding slashes - newline is escaped */ /* Odd number of preceding slashes - newline is escaped */
if (slashes & 1) { if (slashes & 1) {
strcpy(eol-1, eol); overlapping_strcpy(eol - 1, eol);
eol = strchr(eol, '\n'); eol = strchr(eol, '\n');
goto next; goto next;
} }

View File

@ -540,6 +540,7 @@ ssize_t recv_from_to(int fd, void *buf, size_t len, int flags,
char *xstrdup(const char *s) FAST_FUNC; char *xstrdup(const char *s) FAST_FUNC;
char *xstrndup(const char *s, int n) FAST_FUNC; char *xstrndup(const char *s, int n) FAST_FUNC;
void overlapping_strcpy(char *dst, const char *src) FAST_FUNC;
char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC; char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC;
/* Guaranteed to NOT be a macro (smallest code). Saves nearly 2k on uclibc. /* Guaranteed to NOT be a macro (smallest code). Saves nearly 2k on uclibc.
* But potentially slow, don't use in one-billion-times loops */ * But potentially slow, don't use in one-billion-times loops */

View File

@ -1552,7 +1552,7 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li
vi_case(CTRL('U')|vbit:) vi_case(CTRL('U')|vbit:)
/* Control-U -- Clear line before cursor */ /* Control-U -- Clear line before cursor */
if (cursor) { if (cursor) {
strcpy(command, command + cursor); overlapping_strcpy(command, command + cursor);
command_len -= cursor; command_len -= cursor;
redraw(cmdedit_y, command_len); redraw(cmdedit_y, command_len);
} }

View File

@ -161,7 +161,7 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
int n = strspn(line, delims); int n = strspn(line, delims);
if (n) { if (n) {
ii -= n; ii -= n;
strcpy(line, line + n); overlapping_strcpy(line, line + n);
} }
// cut trailing // cut trailing
if (ii) { if (ii) {

View File

@ -16,3 +16,12 @@ char* FAST_FUNC safe_strncpy(char *dst, const char *src, size_t size)
dst[--size] = '\0'; dst[--size] = '\0';
return strncpy(dst, src, size); return strncpy(dst, src, size);
} }
/* Like strcpy but can copy overlapping strings. */
void FAST_FUNC overlapping_strcpy(char *dst, const char *src)
{
while ((*dst = *src) != '\0') {
dst++;
src++;
}
}

View File

@ -800,7 +800,7 @@ int nmeter_main(int argc, char **argv)
if (!cur) if (!cur)
break; break;
if (cur[1] == '%') { // %% if (cur[1] == '%') { // %%
strcpy(cur, cur+1); overlapping_strcpy(cur, cur + 1);
cur++; cur++;
goto again; goto again;
} }