Skip to contentSkip to conversation

dtc: Simplify asm_emit_string() implementation

PATCH In review Nicolas Iooss 3 participants 12 messages Mar 4, 2017 – Mar 9, 2017 7 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 dtc: Simplify asm_emit_string() implementation

Using %.*s format helps making asm_emit_string() not modify its "str" parameter.

While at it, constify the "str" parameter of bin_emit_string() and asm_emit_string(), as these function no longer modify it.

---
 flattree.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
flattree.c +7 -15
index ebac548b3fa8..d8a118258e01 100644
@@ -49,7 +49,7 @@ static struct version_info {
 
 struct emitter {
 	void (*cell)(void *, cell_t);
-	void (*string)(void *, char *, int);
+	void (*string)(void *, const char *, int);
 	void (*align)(void *, int);
 	void (*data)(void *, struct data);
 	void (*beginnode)(void *, struct label *labels);
@@ -64,7 +64,7 @@ static void bin_emit_cell(void *e, cell_t val)
 	*dtbuf = data_append_cell(*dtbuf, val);
 }
 
-static void bin_emit_string(void *e, char *str, int len)
+static void bin_emit_string(void *e, const char *str, int len)
 {
 	struct data *dtbuf = e;
 
@@ -144,22 +144,14 @@ static void asm_emit_cell(void *e, cell_t val)
 		(val >> 8) & 0xff, val & 0xff);
 }
 
-static void asm_emit_string(void *e, char *str, int len)
+static void asm_emit_string(void *e, const char *str, int len)
 {
 	FILE *f = e;
-	char c = 0;
 
-	if (len != 0) {
-		/* XXX: ewww */
-		c = str[len];
-		str[len] = '\0';
-	}
-
-	fprintf(f, "\t.string\t\"%s\"\n", str);
-
-	if (len != 0) {
-		str[len] = c;
-	}
+	if (len != 0)
+		fprintf(f, "\t.string\t\"%.*s\"\n", len, str);
+	else
+		fprintf(f, "\t.string\t\"%s\"\n", str);
 }
 
 static void asm_emit_align(void *e, int a)
Signature · 1 line
2.11.1

A message that is not in the archive (sent off-list or not yet received): 20170304132647.23286-1-nicolas.iooss_linux-oWGTIYur0i8@public.gmane.org

#2 lore raw

PATCH 2/5 libfdt: overlay: Check the value of the right variable

overlay_update_local_node_references() saves the result of fdt_subnode_offset() into variable tree_child but checks for variable ret afterwards. As this does not make sense, check tree_child instead of ret.

This bug has been found by compiling with clang. The compiler reported the following warning:

    libfdt/fdt_overlay.c:275:7: error: variable 'ret' may be
    uninitialized when used here
          [-Werror,-Wconditional-uninitialized]
                    if (ret == -FDT_ERR_NOTFOUND)
                        ^~~
    libfdt/fdt_overlay.c:210:9: note: initialize the variable 'ret' to
    silence this
          warning
            int ret;
                   ^
                    = 0
---
 libfdt/fdt_overlay.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/fdt_overlay.c +1 -1
index 56cb70ed4445..b2fb5d970ccc 100644
@@ -272,7 +272,7 @@ static int overlay_update_local_node_references(void *fdto,
 
 		tree_child = fdt_subnode_offset(fdto, tree_node,
 						fixup_child_name);
-		if (ret == -FDT_ERR_NOTFOUND)
+		if (tree_child == -FDT_ERR_NOTFOUND)
 			return -FDT_ERR_BADOVERLAY;
 		if (tree_child < 0)
 			return tree_child;
Signature · 1 line
2.11.1

A message that is not in the archive (sent off-list or not yet received): 20170304132647.23286-2-nicolas.iooss_linux-oWGTIYur0i8@public.gmane.org

Reviewed-by
#4 lore raw

Re: [PATCH 2/5] libfdt: overlay: Check the value of the right variable

Quoted 20 lines · Nicolas Iooss

overlay_update_local_node_references() saves the result of fdt_subnode_offset() into variable tree_child but checks for variable ret afterwards. As this does not make sense, check tree_child instead of ret.

This bug has been found by compiling with clang. The compiler reported the following warning:

    libfdt/fdt_overlay.c:275:7: error: variable 'ret' may be
    uninitialized when used here
          [-Werror,-Wconditional-uninitialized]
                    if (ret == -FDT_ERR_NOTFOUND)
                        ^~~
    libfdt/fdt_overlay.c:210:9: note: initialize the variable 'ret' to
    silence this
          warning
            int ret;
                   ^
                    = 0
Quoted 4 lines
---
 libfdt/fdt_overlay.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

This suggests we need a new test case.

Quoted 21 lines
libfdt/fdt_overlay.c +1 -2
index 56cb70ed4445..b2fb5d970ccc 100644
@@ -272,7 +272,7 @@ static int overlay_update_local_node_references(void *fdto,
 
                tree_child = fdt_subnode_offset(fdto, tree_node,
                                                fixup_child_name);
-               if (ret == -FDT_ERR_NOTFOUND)
+               if (tree_child == -FDT_ERR_NOTFOUND)
                        return -FDT_ERR_BADOVERLAY;
                if (tree_child < 0)
                        return tree_child;
--

2.11.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CAPnjgZ2Lon3e-NV-FH76QpXFP2tAcc=Ex48hG7OMQxbcjzBfig-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#6 lore raw

Re: [PATCH 2/5] libfdt: overlay: Check the value of the right variable

Quoted 21 lines · Simon Glass
Quoted 20 lines · Nicolas Iooss

overlay_update_local_node_references() saves the result of fdt_subnode_offset() into variable tree_child but checks for variable ret afterwards. As this does not make sense, check tree_child instead of ret.

This bug has been found by compiling with clang. The compiler reported the following warning:

    libfdt/fdt_overlay.c:275:7: error: variable 'ret' may be
    uninitialized when used here
          [-Werror,-Wconditional-uninitialized]
                    if (ret == -FDT_ERR_NOTFOUND)
                        ^~~
    libfdt/fdt_overlay.c:210:9: note: initialize the variable 'ret' to
    silence this
          warning
            int ret;
                   ^
                    = 0
Quoted 8 lines
Quoted 4 lines
---
 libfdt/fdt_overlay.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

This suggests we need a new test case.

True. I've already merged this but adding a testcase as a followup would be most appreciated.

Quoted 16 lines
Quoted 16 lines
libfdt/fdt_overlay.c +1 -1
index 56cb70ed4445..b2fb5d970ccc 100644
@@ -272,7 +272,7 @@ static int overlay_update_local_node_references(void *fdto,
 
                tree_child = fdt_subnode_offset(fdto, tree_node,
                                                fixup_child_name);
-               if (ret == -FDT_ERR_NOTFOUND)
+               if (tree_child == -FDT_ERR_NOTFOUND)
                        return -FDT_ERR_BADOVERLAY;
                if (tree_child < 0)
                        return tree_child;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#7 lore raw

PATCH 3/5 tests: Make realloc_fdt() really allocate *fdt

In reply to a message not in the archive

This bug has been found by using clang Static Analyzer: it reported that the value stored to fdt was never read.

---
 tests/sw_tree1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/sw_tree1.c +2 -2
index 6d4c53102967..4887dc3e1172 100644
@@ -42,14 +42,14 @@ static void realloc_fdt(void **fdt, size_t *size, bool created)
 	switch (alloc_mode) {
 	case FIXED:
 		if (!(*fdt))
-			fdt = xmalloc(*size);
+			*fdt = xmalloc(*size);
 		else
 			FAIL("Ran out of space");
 		return;
 
 	case RESIZE:
 		if (!(*fdt)) {
-			fdt = xmalloc(SPACE);
+			*fdt = xmalloc(SPACE);
 		} else if (*size < SPACE) {
 			*size += 1;
 			fdt_resize(*fdt, *fdt, *size);
Signature · 1 line
2.11.1

A message that is not in the archive (sent off-list or not yet received): 20170304132647.23286-3-nicolas.iooss_linux-oWGTIYur0i8@public.gmane.org

Reviewed-by
#9 lore raw

Re: [PATCH 3/5] tests: Make realloc_fdt() really allocate *fdt

Quoted 7 lines · Nicolas Iooss

This bug has been found by using clang Static Analyzer: it reported that the value stored to fdt was never read.

---
 tests/sw_tree1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

I suggest changing the code in main() so it starts with a 0 size. Then the test would catch this problem.

Quoted 29 lines
tests/sw_tree1.c +2 -3
index 6d4c53102967..4887dc3e1172 100644
@@ -42,14 +42,14 @@ static void realloc_fdt(void **fdt, size_t *size, bool created)
        switch (alloc_mode) {
        case FIXED:
                if (!(*fdt))
-                       fdt = xmalloc(*size);
+                       *fdt = xmalloc(*size);
                else
                        FAIL("Ran out of space");
                return;
 
        case RESIZE:
                if (!(*fdt)) {
-                       fdt = xmalloc(SPACE);
+                       *fdt = xmalloc(SPACE);
                } else if (*size < SPACE) {
                        *size += 1;
                        fdt_resize(*fdt, *fdt, *size);
--

2.11.1

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CAPnjgZ0=jfYeYZSZUeQRf00wyHaQwDBRYvqAQqO6Bu0B2VA5NA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#11 lore raw

Re: [PATCH 3/5] tests: Make realloc_fdt() really allocate *fdt

Quoted 9 lines · Simon Glass
Quoted 7 lines · Nicolas Iooss

This bug has been found by using clang Static Analyzer: it reported that the value stored to fdt was never read.

---
 tests/sw_tree1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 4 lines

I suggest changing the code in main() so it starts with a 0 size. Then the test would catch this problem.

Ah, yes, I was going to suggest this when I reviewed the series, but then I forgot.

Quoted 24 lines
Quoted 24 lines
tests/sw_tree1.c +2 -2
index 6d4c53102967..4887dc3e1172 100644
@@ -42,14 +42,14 @@ static void realloc_fdt(void **fdt, size_t *size, bool created)
        switch (alloc_mode) {
        case FIXED:
                if (!(*fdt))
-                       fdt = xmalloc(*size);
+                       *fdt = xmalloc(*size);
                else
                        FAIL("Ran out of space");
                return;
 
        case RESIZE:
                if (!(*fdt)) {
-                       fdt = xmalloc(SPACE);
+                       *fdt = xmalloc(SPACE);
                } else if (*size < SPACE) {
                        *size += 1;
                        fdt_resize(*fdt, *fdt, *size);
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#12 lore raw

PATCH 4/5 fdtget: Use @return to document the return value

In reply to a message not in the archive
---
 fdtget.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fdtget.c +1 -1
index fb9d0e1730d7..2982a3e7d960 100644
@@ -245,7 +245,7 @@ static int show_data_for_item(const void *blob, struct display_info *disp,
  * @param filename	Filename of blob file
  * @param arg		List of arguments to process
  * @param arg_count	Number of arguments
- * @param return 0 if ok, -ve on error
+ * @return 0 if ok, -ve on error
  */
 static int do_fdtget(struct display_info *disp, const char *filename,
 		     char **arg, int arg_count, int args_per_step)
Signature · 1 line
2.11.1

A message that is not in the archive (sent off-list or not yet received): 20170304132647.23286-4-nicolas.iooss_linux-oWGTIYur0i8@public.gmane.org

#15 lore raw

PATCH 5/5 fdtput: Remove star from value_len documentation

In reply to a message not in the archive

When clang checks the documentation tags (with -Wdocumentation flag), it reports the following warning:

fdtput.c:70:11: error: parameter '*value_len' not found in the function declaration [-Werror,-Wdocumentation]
* @param *value_len Returns length of value encoded
^~~~~~~~~~
fdtput.c:70:11: note: did you mean 'value_len'?
* @param *value_len Returns length of value encoded
^~~~~~~~~~
value_len

As this sounds reasonable, remove the star from the documentation tag.

---
 fdtput.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fdtput.c +1 -1
index db65e9613f29..971199813962 100644
@@ -67,7 +67,7 @@ static void report_error(const char *name, int namelen, int err)
  * @param arg		List of arguments from command line
  * @param arg_count	Number of arguments (may be 0)
  * @param valuep	Returns buffer containing value
- * @param *value_len	Returns length of value encoded
+ * @param value_len	Returns length of value encoded
  */
 static int encode_value(struct display_info *disp, char **arg, int arg_count,
 			char **valuep, int *value_len)
Signature · 1 line
2.11.1

A message that is not in the archive (sent off-list or not yet received): 20170304132647.23286-5-nicolas.iooss_linux-oWGTIYur0i8@public.gmane.org

Reviewed-by
#17 lore raw

Re: [PATCH 5/5] fdtput: Remove star from value_len documentation

Quoted 12 lines · Nicolas Iooss

When clang checks the documentation tags (with -Wdocumentation flag), it reports the following warning:

fdtput.c:70:11: error: parameter '*value_len' not found in the function declaration [-Werror,-Wdocumentation]
* @param *value_len Returns length of value encoded
^~~~~~~~~~
fdtput.c:70:11: note: did you mean 'value_len'?
* @param *value_len Returns length of value encoded
^~~~~~~~~~
value_len

Quoted 6 lines

As this sounds reasonable, remove the star from the documentation tag.

---
 fdtput.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#18 lore raw
In reply to a message not in the archive
Quoted 7 lines · Nicolas Iooss

Using %.*s format helps making asm_emit_string() not modify its "str" parameter.

While at it, constify the "str" parameter of bin_emit_string() and asm_emit_string(), as these function no longer modify it.

All applued, thanks.

Quoted 54 lines
---
 flattree.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
flattree.c +7 -15
index ebac548b3fa8..d8a118258e01 100644
@@ -49,7 +49,7 @@ static struct version_info {
 
 struct emitter {
 	void (*cell)(void *, cell_t);
-	void (*string)(void *, char *, int);
+	void (*string)(void *, const char *, int);
 	void (*align)(void *, int);
 	void (*data)(void *, struct data);
 	void (*beginnode)(void *, struct label *labels);
@@ -64,7 +64,7 @@ static void bin_emit_cell(void *e, cell_t val)
 	*dtbuf = data_append_cell(*dtbuf, val);
 }
 
-static void bin_emit_string(void *e, char *str, int len)
+static void bin_emit_string(void *e, const char *str, int len)
 {
 	struct data *dtbuf = e;
 
@@ -144,22 +144,14 @@ static void asm_emit_cell(void *e, cell_t val)
 		(val >> 8) & 0xff, val & 0xff);
 }
 
-static void asm_emit_string(void *e, char *str, int len)
+static void asm_emit_string(void *e, const char *str, int len)
 {
 	FILE *f = e;
-	char c = 0;
 
-	if (len != 0) {
-		/* XXX: ewww */
-		c = str[len];
-		str[len] = '\0';
-	}
-
-	fprintf(f, "\t.string\t\"%s\"\n", str);
-
-	if (len != 0) {
-		str[len] = c;
-	}
+	if (len != 0)
+		fprintf(f, "\t.string\t\"%.*s\"\n", len, str);
+	else
+		fprintf(f, "\t.string\t\"%s\"\n", str);
 }
 
 static void asm_emit_align(void *e, int a)
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson