Skip to contentSkip to conversation

tools/xl: Fix trim() following c/s e316316 "xl: Rewrite trim()"

PATCH In review Andrew Cooper 4 participants 7 messages Jul 16, 2015 – Jul 17, 2015

lore
Share & tools
lore
#0 lore raw

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.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
---
 tools/libxl/xl_cmdimpl.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/libxl/xl_cmdimpl.c +5 -3
index 37d4af6..0cc9f8b 100644
@@ -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
Acked-by on v1 1/1
#1 lore raw
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.

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
index 37d4af6..0cc9f8b 100644

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
#2 lore raw
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.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC 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
index 37d4af6..0cc9f8b 100644
@@ -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 -0
     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?

I can, but why in particular? It is adjacent to the declaration of len_nonnull.

~Andrew

#3 lore raw
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.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC 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
index 37d4af6..0cc9f8b 100644
@@ -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 -0
     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?

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

#4 lore raw

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.

#5 lore raw
In reply to Wei Liu
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.

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
index 37d4af6..0cc9f8b 100644

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 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.