1 /* $NetBSD: truncated_string.c,v 1.1.1.1 2019/12/22 12:34:06 skrll Exp $ */
2
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5 * libfdt - Flat Device Tree manipulation
6 * Testcase for misbehaviour on a truncated string
7 * Copyright (C) 2018 David Gibson, IBM Corporation.
8 */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdint.h>
14
15 #include <libfdt.h>
16
17 #include "tests.h"
18 #include "testdata.h"
19
main(int argc,char * argv[])20 int main(int argc, char *argv[])
21 {
22 void *fdt = &truncated_string;
23 const struct fdt_property *good, *bad;
24 int off, len;
25 const char *name;
26
27 test_init(argc, argv);
28
29 vg_prepare_blob(fdt, fdt_totalsize(fdt));
30
31 off = fdt_first_property_offset(fdt, 0);
32 good = fdt_get_property_by_offset(fdt, off, NULL);
33
34 off = fdt_next_property_offset(fdt, off);
35 bad = fdt_get_property_by_offset(fdt, off, NULL);
36
37 if (fdt32_to_cpu(good->len) != 0)
38 FAIL("Unexpected length for good property");
39 name = fdt_get_string(fdt, fdt32_to_cpu(good->nameoff), &len);
40 if (!name)
41 FAIL("fdt_get_string() failed on good property: %s",
42 fdt_strerror(len));
43 if (len != 4)
44 FAIL("fdt_get_string() returned length %d (not 4) on good property",
45 len);
46 if (!streq(name, "good"))
47 FAIL("fdt_get_string() returned \"%s\" (not \"good\") on good property",
48 name);
49
50 if (fdt32_to_cpu(bad->len) != 0)
51 FAIL("Unexpected length for bad property\n");
52 name = fdt_get_string(fdt, fdt32_to_cpu(bad->nameoff), &len);
53 if (name)
54 FAIL("fdt_get_string() succeeded incorrectly on bad property");
55 else if (len != -FDT_ERR_TRUNCATED)
56 FAIL("fdt_get_string() gave unexpected error on bad property: %s",
57 fdt_strerror(len));
58
59 /* Make sure the 'good' property breaks correctly if we
60 * truncate the strings section */
61 fdt_set_size_dt_strings(fdt, fdt32_to_cpu(good->nameoff) + 4);
62 name = fdt_get_string(fdt, fdt32_to_cpu(good->nameoff), &len);
63 if (name)
64 FAIL("fdt_get_string() succeeded incorrectly on mangled property");
65 else if (len != -FDT_ERR_TRUNCATED)
66 FAIL("fdt_get_string() gave unexpected error on mangled property: %s",
67 fdt_strerror(len));
68
69 PASS();
70 }
71