Skip to contentSkip to conversation

readline: avoid memcpy() of overlapping regions

PATCH Applied Nickolai Zeldovich 4 participants 10 messages Jan 7, 2013 – Jan 19, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 readline: avoid memcpy() of overlapping regions

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
readline.c +2 -2
index 5fc9643..aeccc7b 100644
@@ -248,8 +248,8 @@ static void readline_hist_add(ReadLineState *rs, const char *cmdline)
     if (idx == READLINE_MAX_CMDS) {
 	/* Need to get one free slot */
 	free(rs->history[0]);
-	memcpy(rs->history, &rs->history[1],
-	       (READLINE_MAX_CMDS - 1) * sizeof(char *));
+        memmove(rs->history, &rs->history[1],
+                (READLINE_MAX_CMDS - 1) * sizeof(char *));
 	rs->history[READLINE_MAX_CMDS - 1] = NULL;
 	idx = READLINE_MAX_CMDS - 1;
     }
Signature · 1 line
1.7.10.4
#2 lore raw
In reply to Nickolai Zeldovich
Quoted 7 lines · Nickolai Zeldovich

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

I made a slight modification: keep the tab characters since the surrounding code still uses them.

Thanks, applied to the trivial patches tree:
https://github.com/stefanha/qemu/commits/trivial-patches

Stefan

#3 lore raw
Quoted 11 lines · Stefan Hajnoczi
Quoted 7 lines · Nickolai Zeldovich

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

I made a slight modification: keep the tab characters since the surrounding code still uses them.

I think tabs should be fixed whenever possible, otherwise we may never get them converted.

Quoted 6 lines

Thanks, applied to the trivial patches tree:
https://github.com/stefanha/qemu/commits/trivial-patches

Stefan

#4 lore raw
Quoted 10 lines · Blue Swirl
Quoted 9 lines · Stefan Hajnoczi
Quoted 7 lines · Nickolai Zeldovich

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 2 lines

I made a slight modification: keep the tab characters since the surrounding code still uses them.

I think tabs should be fixed whenever possible, otherwise we may never get them converted.

Not in a one-line patch when the surrounding lines still use them. It creates a mess.

Stefan

#5 lore raw
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Blue Swirl
Quoted 9 lines · Stefan Hajnoczi
Quoted 7 lines · Nickolai Zeldovich

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I made a slight modification: keep the tab characters since the surrounding code still uses them.

I think tabs should be fixed whenever possible, otherwise we may never get them converted.

Not in a one-line patch when the surrounding lines still use them. It creates a mess.

Only if the reader messes with the tab width settings (and in that case they deserve what they get and they are probably also used to this), otherwise a line with tabs converted to spaces looks exactly the same.

I think this is identical case to braces. If we don't allow mass conversion, conversion as a side effect is the only way and then it should be always followed.

Quoted 2 lines

Stefan

#6 lore raw
Quoted 18 lines · Blue Swirl
Quoted 17 lines · Stefan Hajnoczi
Quoted 15 lines · Blue Swirl
Quoted 11 lines · Stefan Hajnoczi
Quoted 7 lines · Nickolai Zeldovich

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

I made a slight modification: keep the tab characters since the surrounding code still uses them.

I think tabs should be fixed whenever possible, otherwise we may never get them converted.

Quoted 7 lines
Quoted 2 lines

Not in a one-line patch when the surrounding lines still use them. It creates a mess.

Only if the reader messes with the tab width settings (and in that case they deserve what they get and they are probably also used to this), otherwise a line with tabs converted to spaces looks exactly the same.

You are oversimplifying how tab widths work. The author and reader's tab width must match in order for displayed text to appear correctly.

Tell me what you consider the "correct" tab width for readers and I'll find a piece of QEMU code that was authored for a different tab width :).

In other words, it's a mess and best not to perturb it further.

Stefan

#7 lore raw
Quoted 22 lines · Stefan Hajnoczi
Quoted 21 lines · Blue Swirl
Quoted 19 lines · Stefan Hajnoczi
Quoted 10 lines · Blue Swirl
Quoted 9 lines · Stefan Hajnoczi
Quoted 7 lines · Nickolai Zeldovich

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 2 lines

I made a slight modification: keep the tab characters since the surrounding code still uses them.

I think tabs should be fixed whenever possible, otherwise we may never get them converted.

Not in a one-line patch when the surrounding lines still use them. It creates a mess.

Quoted 7 lines
Quoted 4 lines

Only if the reader messes with the tab width settings (and in that case they deserve what they get and they are probably also used to this), otherwise a line with tabs converted to spaces looks exactly the same.

You are oversimplifying how tab widths work. The author and reader's tab width must match in order for displayed text to appear correctly.

Exactly. The default tab width is 8 in all tools and it takes some effort to adjust the tab settings in each tool. For example, how do you change it in less, xterm or cmd.exe?

It is unreasonable and arrogant for an author to assume any other setting used by the reader for tab width, even if this was declared for example at the start of the file.

Perhaps an analogy could be a compressing or encrypting preprocessor for the white space in the code. Without the right tool and correct settings, the reader would not see the white space in the code correctly.

Quoted 3 lines

Tell me what you consider the "correct" tab width for readers and I'll find a piece of QEMU code that was authored for a different tab width :).

8.

Quoted 2 lines

In other words, it's a mess and best not to perturb it further.

No, those messes should be cleaned up, much like braces.

Quoted 2 lines

Stefan

#8 lore raw
Quoted 48 lines · Blue Swirl
Quoted 22 lines · Stefan Hajnoczi
Quoted 21 lines · Blue Swirl
Quoted 19 lines · Stefan Hajnoczi
Quoted 10 lines · Blue Swirl
Quoted 9 lines · Stefan Hajnoczi
Quoted 7 lines · Nickolai Zeldovich

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 2 lines

I made a slight modification: keep the tab characters since the surrounding code still uses them.

I think tabs should be fixed whenever possible, otherwise we may never get them converted.

Not in a one-line patch when the surrounding lines still use them. It creates a mess.

Quoted 7 lines
Quoted 4 lines

Only if the reader messes with the tab width settings (and in that case they deserve what they get and they are probably also used to this), otherwise a line with tabs converted to spaces looks exactly the same.

You are oversimplifying how tab widths work. The author and reader's tab width must match in order for displayed text to appear correctly.

Exactly. The default tab width is 8 in all tools and it takes some effort to adjust the tab settings in each tool. For example, how do you change it in less, xterm or cmd.exe?

It is unreasonable and arrogant for an author to assume any other setting used by the reader for tab width, even if this was declared for example at the start of the file.

Perhaps an analogy could be a compressing or encrypting preprocessor for the white space in the code. Without the right tool and correct settings, the reader would not see the white space in the code correctly.

Quoted 3 lines

Tell me what you consider the "correct" tab width for readers and I'll find a piece of QEMU code that was authored for a different tab width :).

Quoted 6 lines

8.

Quoted 2 lines

In other words, it's a mess and best not to perturb it further.

No, those messes should be cleaned up, much like braces.

Agreed but in cleanup patches or when touching the whole function. Not one line at a time because then we have an even bigger mess.

Stefan

#9 lore raw
Quoted 52 lines · Stefan Hajnoczi
Quoted 51 lines · Blue Swirl
Quoted 22 lines · Stefan Hajnoczi
Quoted 21 lines · Blue Swirl
Quoted 19 lines · Stefan Hajnoczi
Quoted 10 lines · Blue Swirl
Quoted 9 lines · Stefan Hajnoczi
Quoted 7 lines · Nickolai Zeldovich

memcpy() for overlapping regions is undefined behavior; use memmove() instead in readline_hist_add().

---
 readline.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 2 lines

I made a slight modification: keep the tab characters since the surrounding code still uses them.

I think tabs should be fixed whenever possible, otherwise we may never get them converted.

Not in a one-line patch when the surrounding lines still use them. It creates a mess.

Quoted 7 lines
Quoted 4 lines

Only if the reader messes with the tab width settings (and in that case they deserve what they get and they are probably also used to this), otherwise a line with tabs converted to spaces looks exactly the same.

You are oversimplifying how tab widths work. The author and reader's tab width must match in order for displayed text to appear correctly.

Exactly. The default tab width is 8 in all tools and it takes some effort to adjust the tab settings in each tool. For example, how do you change it in less, xterm or cmd.exe?

It is unreasonable and arrogant for an author to assume any other setting used by the reader for tab width, even if this was declared for example at the start of the file.

Perhaps an analogy could be a compressing or encrypting preprocessor for the white space in the code. Without the right tool and correct settings, the reader would not see the white space in the code correctly.

Quoted 3 lines

Tell me what you consider the "correct" tab width for readers and I'll find a piece of QEMU code that was authored for a different tab width :).

8.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

In other words, it's a mess and best not to perturb it further.

No, those messes should be cleaned up, much like braces.

Agreed but in cleanup patches or when touching the whole function. Not one line at a time because then we have an even bigger mess.

So you are advocating cleanup patches which only adjust formatting? How about mass conversion then? Anthony has rejected the similar approach for braces because of the loss of git blame info.

Quoted 2 lines

Stefan