Browse Source

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 <david@gibson.dropbear.id.au>
main
David Gibson 15 years ago committed by Jon Loeliger
parent
commit
c623fe5c21
  1. 1
      tests/Makefile.tests
  2. 59
      tests/extra-terminating-null.c
  3. 11
      tests/extra-terminating-null.dts
  4. 5
      tests/run_tests.sh
  5. 29
      treesource.c

1
tests/Makefile.tests

@ -11,6 +11,7 @@ LIB_TESTS_L = get_mem_rsv \ @@ -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)%)

59
tests/extra-terminating-null.c

@ -0,0 +1,59 @@ @@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include <fdt.h>
#include <libfdt.h>

#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();
}

11
tests/extra-terminating-null.dts

@ -0,0 +1,11 @@ @@ -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";
};

5
tests/run_tests.sh

@ -203,6 +203,9 @@ dtc_tests () { @@ -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 () { @@ -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

29
treesource.c

@ -63,26 +63,20 @@ static void write_propval_string(FILE *f, struct data val) @@ -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) @@ -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))

Loading…
Cancel
Save