File: dwarf_test_errmsg_list.c

package info (click to toggle)
dwarfutils 20180809-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,228 kB
  • sloc: ansic: 63,382; sh: 5,222; cpp: 4,041; makefile: 548; python: 445; awk: 11
file content (282 lines) | stat: -rw-r--r-- 8,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
  Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
  Portions Copyright (C) 2008-2018 David Anderson.  All Rights Reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2.1 of the GNU Lesser General Public License
  as published by the Free Software Foundation.

  This program is distributed in the hope that it would be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  Further, this software is distributed without any warranty that it is
  free of the rightful claim of any third person regarding infringement
  or the like.  Any license provided herein, whether implied or
  otherwise, applies only to this software file.  Patent licenses, if
  any, provided herein do not apply to combinations of this program with
  other software, or any other product whatsoever.

  You should have received a copy of the GNU Lesser General Public
  License along with this program; if not, write the Free Software
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
  USA.

*/

#include "config.h"
#include "dwarf_incl.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>  /* For exit(), strtoul() declaration etc. */
#endif

#include "dwarf_errmsg_list.h"


#define FALSE 0
#define TRUE 1
/* This is just to help localize the error. */
static void
printone(int i)
{
    int arraysize = sizeof(_dwarf_errmsgs) / sizeof(char *);
    if ( i >= arraysize) {
        printf("%d is outside the array! Missing something!\n",i);
    } else {
        printf("%d is: %s\n",i,_dwarf_errmsgs[i]);
    }
}

/* Arbitrary. A much smaller max length value would work. */
#define MAX_NUM_LENGTH 12

/* return TRUE on error */
static int
check_errnum_mismatches(unsigned i)
{
    unsigned nextstop = 0;
    const char *sp = _dwarf_errmsgs[i];
    const char *cp = sp;
    unsigned innit = FALSE;
    unsigned prevchar = 0;
    unsigned value = 0;

    for( ; *cp; cp++) {
        unsigned c = 0;
        c = 0xff & *cp;
        if ( c >= '0' && c <= '9' && !innit
            && prevchar != '(') {
            /* Skip. number part of macro name. */
            prevchar = c;
            continue;
        }
        if ( c >= '0' && c <= '9') {
            value = value * 10;
            value += (c - '0');
            nextstop++;
            if (nextstop > MAX_NUM_LENGTH) {
                break;
            }
            innit = TRUE;
        } else {
            if (innit) {
                break;
            }
            prevchar= c;
        }
    }
    if (innit) {
        if (i != value) {
            return TRUE;
        }
        return FALSE;
    }
    /* There is no number to check. Ignore it. */
    printf("mismatch value %d has no errnum to check %s\n",i,_dwarf_errmsgs[i]);
    return TRUE;
}

/* We don't allow arbitrary DW_DLE line length. */
#define MAXDEFINELINE 200

static int
splmatches(char *base, unsigned baselen,char *test)
{
    if (baselen != strlen(test) ) {
        return FALSE;
    }
    for ( ; *test; ++test,++base) {
        if (*test != *base) {
            return FALSE;
        }
    }
    return TRUE;
}

static void
check_dle_list(const char *path)
{
    /*  The format should be
        #define<space>name<spaces>number<spaces>optional-c-comment
        and we are intentionally quite rigid about it all except
        that the number of spaces before any comment is allowed. */
    char buffer[MAXDEFINELINE];
    unsigned linenum = 0;
    unsigned long prevdefval = 0;
    unsigned foundlast = 0;
    unsigned foundlouser = 0;
    FILE*fin = 0;

    fin = fopen(path, "r");
    if(!fin) {
        printf("Unable to open define list %s\n",path);
        exit(1);
    }
    for(;;++linenum) {
        char *line = 0;
        unsigned linelen = 0;
        char *  curdefname = 0;
        char *  pastname = 0;
        unsigned curdefname_len = 0;
        char *numstart = 0;
        char * endptr = 0;
        unsigned long v = 0;

        line = fgets(buffer,MAXDEFINELINE,fin);
        if(!line) {
            break;
        }
        linelen = strlen(line);
        if (linelen >= (unsigned)(MAXDEFINELINE-1)) {
            printf("define line %u is too long!\n",linenum);
            exit(1);
        }
        if(strncmp(line,"#define DW_DLE_",15)) {
            printf("define line %u has wrong leading chars!\n",linenum);
            exit(1);
        }
        curdefname = line+8;
        /* ASSERT: line ends with NUL byte. */
        for( ; ; curdefname_len++) {
            if (foundlouser) {
                printf("define line %u has  stuff after DW_DLE_LO_USER!\n",
                    linenum);
                exit(1);
            }
            pastname = curdefname +curdefname_len;
            if (!*pastname) {
                /* At end of line. Missing value. */
                printf("define line %u has no number value!\n",linenum);
                exit(1);
            }
            if (*pastname == ' ') {
                /* Ok. Now look for value. */
                numstart = pastname + 1;
                break;
            }
        }
        /* strtoul skips leading whitespace. */
        v = strtoul(numstart,&endptr,0);
        /*  This test is a bit odd. But is valid till
            we decide it is inappropriate. */
        if (v > DW_DLE_LO_USER) {
            printf("define line %u: number value unreasonable. %lu\n",
                linenum,v);
            exit(1);
        }
        if (v == 0 && endptr == numstart) {
            printf("define line %u: number value missing.\n",
                linenum);
            exit(1);
        }
        if (*endptr != ' ' && *endptr != '\n') {
            printf("define line %u: number value terminates oddly\n",
                linenum);
            exit(1);
        }
        if (splmatches(curdefname,curdefname_len,"DW_DLE_LAST")) {
            if (foundlast) {
                printf("duplicated DW_DLE_LAST! line %u\n",linenum);
                exit(1);
            }
            foundlast = 1;
            if (v != prevdefval) {
                printf("Invalid: Last value mismatch! %lu vs %lu\n",
                    v,prevdefval);
            }
        } else if (splmatches(curdefname,curdefname_len,"DW_DLE_LO_USER")) {
            if (!foundlast) {
                printf("error:expected DW_DLE_LO_USER after LAST! line %u\n",
                    linenum);
                exit(1);
            }
            if (foundlouser) {
                printf("Error:duplicated DW_DLE_LO_USER! line %u\n",
                    linenum);
                exit(1);
            }
            foundlouser = 1;
            continue;
        } else {
            if (linenum > 0) {
                if (v != prevdefval+1) {
                    printf("Invalid: Missing value! %lu vs %lu\n",
                        prevdefval,v);
                    exit(1);
                }
            }
            prevdefval = v;
        }
        /* Ignoring rest of line for now. */
    }
    fclose(fin);
}

int
main(int argc, char **argv)
{
    unsigned arraysize = sizeof(_dwarf_errmsgs) / sizeof(char *);
    unsigned i = 0;
    const char *path = 0;

    if (argc != 3) {
        printf("Expected -f <filename> of DW_DLE lines from libdwarf.h");
        exit(1);
    }
    if (strcmp(argv[1],"-f")) {
        printf("Expected -f");
        exit(1);
    }
    path = argv[2];
    check_dle_list(path);


    if (arraysize != (DW_DLE_LAST + 1)) {
        printf("Missing or extra entry in dwarf error strings array"
            " %u expected DW_DLE_LAST+1 %d\n",arraysize, DW_DLE_LAST+1);
        printone(1);
        printone(100);
        printone(200);
        printone(250);
        printone(260);
        printone(262);
        printone(263);
        printone(264);
        printone(265);
        printone(273);
        printone(274);
        printone(275);
        printone(300);
        printone(328);
        exit(1);
    }
    for ( i = 0; i <= DW_DLE_LAST; ++i) {
        if(check_errnum_mismatches(i)) {
            printf("mismatch value %d is: %s\n",i,_dwarf_errmsgs[i]);
            exit(1);
        }
    }
    /* OK. */
    exit(0);
}