From c623fe5c21e0358fc38a4e8ddb0d51379f0733e8 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 9 Sep 2009 14:38:30 +1000 Subject: [PATCH] Fix bug in -Odts with properties containing multiple terminating nulls When in -Odts mode, dtc will not produce correct output for string-like properties which have more than one \0 character at the end of the property's bytestring. In fact, it generates output which is not syntactically correct. This patch fixes the bug, and adds a testcase for future regressions here. Signed-off-by: David Gibson --- tests/Makefile.tests | 1 + tests/extra-terminating-null.c | 59 ++++++++++++++++++++++++++++++++ tests/extra-terminating-null.dts | 11 ++++++ tests/run_tests.sh | 5 ++- treesource.c | 29 ++++++++-------- 5 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 tests/extra-terminating-null.c create mode 100644 tests/extra-terminating-null.dts diff --git a/tests/Makefile.tests b/tests/Makefile.tests index c5ad834..e35afa6 100644 --- a/tests/Makefile.tests +++ b/tests/Makefile.tests @@ -11,6 +11,7 @@ LIB_TESTS_L = get_mem_rsv \ move_and_save mangle-layout nopulate \ open_pack rw_tree1 set_name setprop del_property del_node \ string_escapes references path-references boot-cpuid incbin \ + extra-terminating-null \ dtbs_equal_ordered \ add_subnode_with_nops path_offset_aliases LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%) diff --git a/tests/extra-terminating-null.c b/tests/extra-terminating-null.c new file mode 100644 index 0000000..bb71b1a --- /dev/null +++ b/tests/extra-terminating-null.c @@ -0,0 +1,59 @@ +/* + * libfdt - Flat Device Tree manipulation + * Testcase for properties with more than one terminating null + * Copyright (C) 2009 David Gibson, IBM Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include +#include +#include + +#include +#include + +#include "tests.h" +#include "testdata.h" + +void check_extranull(void *fdt, const char *prop, const char *str, int numnulls) +{ + int len = strlen(str); + char checkbuf[len+numnulls]; + + memset(checkbuf, 0, sizeof(checkbuf)); + memcpy(checkbuf, TEST_STRING_1, len); + + check_getprop(fdt, 0, prop, len+numnulls, checkbuf); +} + +int main(int argc, char *argv[]) +{ + void *fdt; + + test_init(argc, argv); + + fdt = load_blob_arg(argc, argv); + + check_extranull(fdt, "extranull0", TEST_STRING_1, 1); + check_extranull(fdt, "extranull1,1", TEST_STRING_1, 2); + check_extranull(fdt, "extranull1,2", TEST_STRING_1, 2); + check_extranull(fdt, "extranull2,1", TEST_STRING_1, 3); + check_extranull(fdt, "extranull2,2", TEST_STRING_1, 3); + check_extranull(fdt, "extranull2,3", TEST_STRING_1, 3); + check_extranull(fdt, "extranull2,4", TEST_STRING_1, 3); + + PASS(); +} diff --git a/tests/extra-terminating-null.dts b/tests/extra-terminating-null.dts new file mode 100644 index 0000000..b6cc19c --- /dev/null +++ b/tests/extra-terminating-null.dts @@ -0,0 +1,11 @@ +/dts-v1/; + +/ { + extranull0 = "hello world"; + extranull1,1 = "hello world\0"; + extranull1,2 = "hello world", ""; + extranull2,1 = "hello world\0\0"; + extranull2,2 = "hello world", "", ""; + extranull2,3 = "hello world\0", ""; + extranull2,4 = "hello world", "\0"; +}; diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 61508a8..8e57cf5 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -203,6 +203,9 @@ dtc_tests () { run_dtc_test -I dts -O dtb -o dtc_escapes.test.dtb escapes.dts run_test string_escapes dtc_escapes.test.dtb + run_dtc_test -I dts -O dtb -o dtc_extra-terminating-null.test.dtb extra-terminating-null.dts + run_test extra-terminating-null dtc_extra-terminating-null.test.dtb + run_dtc_test -I dts -O dtb -o dtc_references.test.dtb references.dts run_test references dtc_references.test.dtb @@ -252,7 +255,7 @@ dtc_tests () { # Check -Odts mode preserve all dtb information for tree in test_tree1.dtb dtc_tree1.test.dtb dtc_escapes.test.dtb \ - dtc_references.test.dtb; do + dtc_extra-terminating-null.test.dtb dtc_references.test.dtb; do run_dtc_test -I dtb -O dts -o odts_$tree.test.dts $tree run_dtc_test -I dts -O dtb -o odts_$tree.test.dtb odts_$tree.test.dts run_test dtbs_equal_ordered $tree odts_$tree.test.dtb diff --git a/treesource.c b/treesource.c index 1521ff1..cc1751d 100644 --- a/treesource.c +++ b/treesource.c @@ -63,26 +63,20 @@ static void write_propval_string(FILE *f, struct data val) { const char *str = val.val; int i; - int newchunk = 1; struct marker *m = val.markers; assert(str[val.len-1] == '\0'); + while (m && (m->offset == 0)) { + if (m->type == LABEL) + fprintf(f, "%s: ", m->ref); + m = m->next; + } + fprintf(f, "\""); + for (i = 0; i < (val.len-1); i++) { char c = str[i]; - if (newchunk) { - while (m && (m->offset <= i)) { - if (m->type == LABEL) { - assert(m->offset == i); - fprintf(f, "%s: ", m->ref); - } - m = m->next; - } - fprintf(f, "\""); - newchunk = 0; - } - switch (c) { case '\a': fprintf(f, "\\a"); @@ -113,7 +107,14 @@ static void write_propval_string(FILE *f, struct data val) break; case '\0': fprintf(f, "\", "); - newchunk = 1; + while (m && (m->offset < i)) { + if (m->type == LABEL) { + assert(m->offset == (i+1)); + fprintf(f, "%s: ", m->ref); + } + m = m->next; + } + fprintf(f, "\""); break; default: if (isprint(c))