tools/xl: Fix trim() following c/s e316316 "xl: Rewrite trim()"
PATCH 1/1 tools/xl: Fix trim() following c/s e316316 "xl: Rewrite trim()"
c/s e316316 "xl: Rewrite trim()" uses the wrong indirection of 'output', causing memory corruption for all callers.
Introduce a new local variable, making the code more obviously correct.
--- tools/libxl/xl_cmdimpl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
tools/libxl/xl_cmdimpl.c +5 -3
@@ -663,10 +663,12 @@ static void trim(char_predicate_t predicate, const char *input, char **output) ;size_t len_nonnull = after - first; +char *result =xmalloc (len_nonnull +1 ); - *output =xmalloc (len_nonnull +1 ); -memcpy (output, first, len_nonnull); - output[len_nonnull] =0 ; +memcpy (result, first, len_nonnull); + result[len_nonnull] =0 ; + + *output = result; } static int split_string_into_pair(const char *str,
Signature · 1 line
1.7.10.4
Quoted 10 lines · Andrew Cooper
c/s e316316 "xl: Rewrite
trim()" uses the wrong indirection of 'output', causing memory corruption for all callers.Introduce a new local variable, making the code more obviously correct.
Signed-off-by Andrew Cooper <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>CC Wei Liu <[email protected]>
One nit below.
Quoted 9 lines
--- tools/libxl/xl_cmdimpl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)tools/libxl/xl_cmdimpl.c +0
-0
tools/libxl/xl_cmdimpl.c static void trim(char_predicate_t predicate, const char *input, char **output) · line 667
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +1
-0@@ -663,10 +663,12 @@ static void trim(char_predicate_t predicate, const char *input, char **output) ;size_t len_nonnull = after - first; +char *result =xmalloc (len_nonnull +1 );
Can you move the declaration of result to the beginning of this function?
Quoted 12 lines
tools/libxl/xl_cmdimpl.c +4
-3- *output =xmalloc (len_nonnull +1 ); -memcpy (output, first, len_nonnull); - output[len_nonnull] =0 ; +memcpy (result, first, len_nonnull); + result[len_nonnull] =0 ; + + *output = result; } static int split_string_into_pair(const char *str,Signature · 1 line
1.7.10.4
Quoted 26 lines · Wei Liu
Quoted 10 lines · Andrew Cooper
c/s e316316 "xl: Rewrite
trim()" uses the wrong indirection of 'output', causing memory corruption for all callers.Introduce a new local variable, making the code more obviously correct.
Signed-off-by Andrew Cooper <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>CC Wei Liu <[email protected]>Acked-by Wei Liu <[email protected]>One nit below.
Quoted 11 lines
--- tools/libxl/xl_cmdimpl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)tools/libxl/xl_cmdimpl.c +0
-0@@ -663,10 +663,12 @@ static void trim(char_predicate_t predicate, const char *input, char **output) ;
Quoted 5 lines
tools/libxl/xl_cmdimpl.c static void trim(char_predicate_t predicate, const char *input, char **output) · line 667
Quoted 3 lines
tools/libxl/xl_cmdimpl.c +1
-0size_t len_nonnull = after - first; +char *result =xmalloc (len_nonnull +1 );Can you move the declaration of result to the beginning of this function?
I can, but why in particular? It is adjacent to the declaration of len_nonnull.
~Andrew
Quoted 27 lines · Andrew Cooper
Quoted 26 lines · Wei Liu
Quoted 10 lines · Andrew Cooper
c/s e316316 "xl: Rewrite
trim()" uses the wrong indirection of 'output', causing memory corruption for all callers.Introduce a new local variable, making the code more obviously correct.
Signed-off-by Andrew Cooper <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>CC Wei Liu <[email protected]>Acked-by Wei Liu <[email protected]>One nit below.
Quoted 11 lines
--- tools/libxl/xl_cmdimpl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)tools/libxl/xl_cmdimpl.c +0
-0@@ -663,10 +663,12 @@ static void trim(char_predicate_t predicate, const char *input, char **output) ;
Quoted 9 lines
Quoted 5 lines
tools/libxl/xl_cmdimpl.c static void trim(char_predicate_t predicate, const char *input, char **output) · line 667
Quoted 3 lines
tools/libxl/xl_cmdimpl.c +1
-0size_t len_nonnull = after - first; +char *result =xmalloc (len_nonnull +1 );Can you move the declaration of result to the beginning of this function?
I can, but why in particular? It is adjacent to the declaration of
len_nonnull.
C90 forbids this. Not sure how much we care about that and I'm probably bicksheding too much.
Wei.
Quoted 1 line
~Andrew
Wei Liu writes ("Re: [PATCH] tools/xl: Fix trim() following c/s e316316 "xl: Rewrite trim()""):
Quoted 2 lines
C90 forbids this. Not sure how much we care about that and I'm probably bicksheding too much.
libxl/Makefile has -Wno-declaration-after-statement
Ian.
Quoted 34 lines · Wei Liu
Quoted 33 lines · Andrew Cooper
Quoted 31 lines · Wei Liu
Quoted 10 lines · Andrew Cooper
c/s e316316 "xl: Rewrite
trim()" uses the wrong indirection of 'output', causing memory corruption for all callers.Introduce a new local variable, making the code more obviously correct.
Signed-off-by Andrew Cooper <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>CC Wei Liu <[email protected]>Acked-by Wei Liu <[email protected]>One nit below.
Quoted 9 lines
--- tools/libxl/xl_cmdimpl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)tools/libxl/xl_cmdimpl.c +0
-0tools/libxl/xl_cmdimpl.c static void trim(char_predicate_t predicate, const char *input, char **output) · line 667
Quoted 5 lines
tools/libxl/xl_cmdimpl.c +1
-0@@ -663,10 +663,12 @@ static void trim(char_predicate_t predicate, const char *input, char **output) ;size_t len_nonnull = after - first; +char *result =xmalloc (len_nonnull +1 );Can you move the declaration of result to the beginning of this function?
Quoted 6 lines
Quoted 3 lines
I can, but why in particular? It is adjacent to the declaration of
len_nonnull.C90 forbids this. Not sure how much we care about that and I'm probably bicksheding too much.
We made an explicit exception for this in libxl, via the use of -Wno-declaration-after-statement.
IIRC because some of the GC macros require it, but it has since become allowed more generally (although not documented it seems).
In this particular case having result declared right after len_nonnull makes sense.
I was about to Ack + apply but I see Ian beat me to it.
Ian.
Andrew Cooper writes ("[PATCH] tools/xl: Fix trim() following c/s e316316 "xl: Rewrite trim()""):
Quoted 5 lines
c/s e316316 "xl: Rewrite
trim()" uses the wrong indirection of 'output', causing memory corruption for all callers.Introduce a new local variable, making the code more obviously correct.
Gah, thanks.
Ian.