Skip to content

Commit 61e39cf

Browse files
committed
Now 'make check' and the like protect against
errors in setting up DW_LNAME data and code. modified: test/CMakeLists.txt modified: test/Makefile.am modified: test/meson.build modified: test/test_lname.c
1 parent 6000fa7 commit 61e39cf

File tree

4 files changed

+52
-74
lines changed

4 files changed

+52
-74
lines changed

test/CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if (DO_TESTING)
1111
target_compile_options(selfteststring PRIVATE ${DW_FWALL})
1212
add_test(NAME selfteststring COMMAND selfteststring)
1313
endif()
14+
1415
if (DO_TESTING)
1516
set_source_group(TESTEXTRASTRING "Source Files"
1617
${PROJECT_SOURCE_DIR}/test/test_extra_flag_strings.c
@@ -39,7 +40,7 @@ if (DO_TESTING)
3940
${PROJECT_SOURCE_DIR}/src/lib/libdwarf/dwarf_debuglink.h
4041
${PROJECT_SOURCE_DIR}/src/lib/libdwarf/dwarf_debuglink.c
4142
${PROJECT_SOURCE_DIR}/src/lib/libdwarf/dwarf_error.h)
42-
add_executable(selftest_linkedtopath ${TESTEXTRASTRING})
43+
add_executable(selftest_linkedtopath ${TESTLINKEDTOPATH})
4344
target_compile_definitions(selftest_linkedtopath PRIVATE
4445
${DW_LIBDWARF_STATIC})
4546
target_compile_options(selftest_linkedtopath PRIVATE
@@ -51,6 +52,17 @@ if (DO_TESTING)
5152
add_test(NAME selftest_linkedtopath COMMAND selftest_linkedtopath)
5253
endif()
5354

55+
if (DO_TESTING)
56+
set_source_group(TESTLNAME "Source Files"
57+
${PROJECT_SOURCE_DIR}/test/test_lname.c)
58+
add_executable(selftestlname ${TESTLNAME})
59+
target_compile_options(selftestlname PRIVATE
60+
"-DTESTING -DLIBDWARF_BUILD" )
61+
target_compile_options(selftestlname PRIVATE ${DW_FWALL})
62+
add_test(NAME selftestlname COMMAND
63+
selftestlname -f "${PROJECT_SOURCE_DIR}")
64+
endif()
65+
5466
if (DO_TESTING)
5567
set_source_group(GETOPTEST_SOURCES "Source Files"
5668
${PROJECT_SOURCE_DIR}/test/test_getopt.c

test/Makefile.am

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ MAINTAINERCLEANFILES = Makefile.in\
3535
test_ignoresec.trs \
3636
test_linkedtopath.log \
3737
test_linkedtopath.trs \
38+
test_lname.trs\
39+
test_lname.log\
3840
test_macrocheck.log \
3941
test_macrocheck.trs \
4042
test_makenametest.log \
@@ -66,6 +68,7 @@ TESTS = test_canonical \
6668
test_ignoresec \
6769
test_int64_test \
6870
test_linkedtopath \
71+
test_lname \
6972
test_macrocheck \
7073
test_makenametest \
7174
test_regex \
@@ -86,6 +89,7 @@ check_PROGRAMS = test_canonical \
8689
test_ignoresec \
8790
test_int64_test \
8891
test_linkedtopath \
92+
test_lname \
8993
test_macrocheck \
9094
test_makenametest \
9195
test_regex \
@@ -196,6 +200,15 @@ test_linkedtopath_CPPFLAGS = -DTESTING \
196200
-I$(top_builddir) \
197201
-I$(top_srcdir)/src/lib/libdwarf
198202

203+
test_lname_SOURCES = test_lname.c
204+
test_lname_CFLAGS = $(DWARF_CFLAGS_WARN) -DTESTING
205+
test_lname_CPPFLAGS = -DTESTING \
206+
-DLIBDWARF_BUILD \
207+
-I$(top_srcdir) \
208+
-I$(top_builddir) \
209+
-I$(top_srcdir)/src/lib/libdwarf
210+
211+
199212
test_macrocheck_SOURCES = test_macrocheck.c \
200213
$(top_srcdir)/src/bin/dwarfdump/dd_esb.c \
201214
$(top_srcdir)/src/bin/dwarfdump/dd_tsearchbal.c

test/meson.build

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ endforeach
110110
argstests = [
111111
[
112112
'test_errmsglist.c',
113-
'../src/lib/libdwarf/dwarf_safe_strcpy.c',
113+
'../src/lib/libdwarf/dwarf_safe_strcpy.c'
114+
],
115+
[
116+
'test_lname.c'
114117
]
115118
]
116119

test/test_lname.c

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,19 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4040
#include <stdlib.h> /* atol() exit() getenv() */
4141
#include <string.h> /* strcmp() strlen() strncmp() */
4242

43-
#include "libdwarf.h"
44-
#include "libdwarf_private.h"
45-
4643
#define TRUE 1
4744
#define FALSE 0
4845

4946
/* We don't allow arbitrary DW_DLE line length. */
5047
#define MAXDEFINELINE 1000
5148
static char buffer[MAXDEFINELINE];
52-
static char buffer2[MAXDEFINELINE];
5349

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

5753
/* Increase this when the actual number of DW_LNAME_name
5854
instances approaches this number. */
59-
#define MAX_LNAME_LEN 20
55+
#define MAX_LNAME_LEN 30
6056
#define LNAME_INSTANCE_MAX 100
6157
struct instance_s {
6258
char name[MAX_LNAME_LEN];
@@ -69,7 +65,7 @@ int header_count;
6965
struct instance_s csource[LNAME_INSTANCE_MAX];
7066
int csource_count;
7167

72-
void
68+
static void
7369
_dwarf_safe_strcpy(char *out,
7470
size_t outlen,
7571
const char *in_s,
@@ -100,7 +96,7 @@ _dwarf_safe_strcpy(char *out,
10096
*cpo = 0;
10197
}
10298

103-
void
99+
static void
104100
check_for_dup(struct instance_s *ary,int count,
105101
char *name, const char *msg)
106102
{
@@ -117,7 +113,7 @@ check_for_dup(struct instance_s *ary,int count,
117113
}
118114

119115
/* return TRUE on error */
120-
static int
116+
static void
121117
check_hdr_match(void)
122118
{
123119
int k = 0;
@@ -135,8 +131,8 @@ check_hdr_match(void)
135131
foundh = FALSE;
136132
foundc = FALSE;
137133

138-
for ( ; l < csource_count; ++l) {
139-
struct instance_s * curc = &csource[k];
134+
for (l = 0 ; l < csource_count; ++l) {
135+
struct instance_s * curc = &csource[l];
140136

141137
if (strcmp(curh->name,curc->name)) {
142138
continue;
@@ -145,18 +141,19 @@ check_hdr_match(void)
145141
foundh= TRUE;
146142
curc->count++;
147143
if (curc->count > 1) {
148-
printf("FAIL DUP %s in csource\n",
149-
curc->name);
144+
printf("FAIL DUP %s in csource count %d\n",
145+
curc->name,curc->count);
150146
exit(EXIT_FAILURE);
151147
}
148+
break;
152149
}
153150
if (!foundc) {
154-
printf("FAIL to find %s in csource\n",
151+
printf("FAIL to find curc %s in csource\n",
155152
curh->name);
156153
exit(EXIT_FAILURE);
157154
}
158155
if (!foundh) {
159-
printf("FAIL to find %s in csource\n",
156+
printf("FAIL to find curh %s in csource\n",
160157
curh->name);
161158
exit(EXIT_FAILURE);
162159
}
@@ -172,15 +169,11 @@ read_lname_csrc(char *path)
172169
/* The format should be
173170
case<space>name<colon> */
174171
unsigned linenum = 0;
175-
unsigned long prevdefval = 0;
176-
unsigned cur_dle_line = 0;
177-
unsigned foundlast = 0;
178-
unsigned foundlouser = 0;
179172
FILE*fin = 0;
180173

181174
fin = fopen(path, "r");
182175
if (!fin) {
183-
printf("Unable to open define list to read %s\n",path);
176+
printf("Unable to open define csource to read %s\n",path);
184177
exit(EXIT_FAILURE);
185178
}
186179
for ( ;;++linenum) {
@@ -189,9 +182,6 @@ read_lname_csrc(char *path)
189182
char *curdefname = 0;
190183
char *pastname = 0;
191184
unsigned curdefname_len = 0;
192-
char *numstart = 0;
193-
char *endptr = 0;
194-
unsigned long v = 0;
195185
int name_ok = FALSE;
196186

197187
line = fgets(buffer,MAXDEFINELINE,fin);
@@ -203,26 +193,27 @@ read_lname_csrc(char *path)
203193
line[linelen-1] = 0;
204194
--linelen;
205195
if (linelen >= (unsigned)(MAXDEFINELINE-1)) {
206-
printf("define line %u is too long!\n",linenum);
196+
printf("case line %u is too long!\n",linenum);
207197
exit(EXIT_FAILURE);
208198
}
209199
if (strncmp(line," case DW_LNAME_",18)) {
210200
/* Skip the non- case DW_LNAME lines */
211201
continue;
212202
}
213-
curdefname = line+10;
203+
curdefname = line+9;
214204
/* ASSERT: line ends with NUL byte. */
215205
for ( ; ; curdefname_len++) {
216206
pastname = curdefname +curdefname_len;
217207
if (!*pastname) {
218208
/* At end of line. Missing value. */
219-
printf("define line %u of %s: has no number value!\n",
209+
printf("WARNING: csrc case name has no comment %d %s\n",
220210
linenum,path);
221-
exit(EXIT_FAILURE);
211+
continue;
222212
}
223213
if (*pastname == ' ' || *pastname == ':') {
224214
/* Ok. Now insert into table. */
225215
name_ok = TRUE;
216+
--curdefname_len;
226217
*pastname = 0;
227218
break;
228219
}
@@ -232,12 +223,7 @@ read_lname_csrc(char *path)
232223
csource_count,__LINE__);
233224
exit(EXIT_FAILURE);
234225
}
235-
if (header_count +1 != v) {
236-
printf(" Fail checking defines: count %d v %d\n",
237-
header_count,(int)v);
238-
exit(EXIT_FAILURE);
239-
}
240-
if (strlen(curdefname)+1 <= MAX_LNAME_LEN) {
226+
if (strlen(curdefname)+1 >= MAX_LNAME_LEN) {
241227
printf("Name %s is too long for table at %u\n",
242228
curdefname,
243229
(unsigned)strlen(curdefname));
@@ -264,15 +250,11 @@ read_lname_hdr(char *path)
264250
and we are intentionally quite rigid about it all except
265251
that the number of spaces before any comment is allowed. */
266252
unsigned linenum = 0;
267-
unsigned long prevdefval = 0;
268-
unsigned cur_dle_line = 0;
269-
unsigned foundlast = 0;
270-
unsigned foundlouser = 0;
271253
FILE*fin = 0;
272254

273255
fin = fopen(path, "r");
274256
if (!fin) {
275-
printf("Unable to open define list to read %s\n",path);
257+
printf("Unable to open define dwarf.h to read %s\n",path);
276258
exit(EXIT_FAILURE);
277259
}
278260
for ( ;;++linenum) {
@@ -298,19 +280,13 @@ read_lname_hdr(char *path)
298280
printf("define line %u is too long!\n",linenum);
299281
exit(EXIT_FAILURE);
300282
}
301-
if (strncmp(line,"#define DW_LNAME__",21)) {
283+
if (strncmp(line,"#define DW_LNAME_",17)) {
302284
/* Skip the non- DW_DLE_ lines */
303285
continue;
304286
}
305287
curdefname = line+8;
306288
/* ASSERT: line ends with NUL byte. */
307289
for ( ; ; curdefname_len++) {
308-
if (foundlouser) {
309-
printf("define line %u has stuff after "
310-
"DW_DLE_LO_USER!\n",
311-
linenum);
312-
exit(EXIT_FAILURE);
313-
}
314290
pastname = curdefname +curdefname_len;
315291
if (!*pastname) {
316292
/* At end of line. Missing value. */
@@ -353,12 +329,12 @@ read_lname_hdr(char *path)
353329
linenum,e,e,line);
354330
exit(EXIT_FAILURE);
355331
}
356-
if (header_count +1 != v) {
332+
if ((header_count +1) != (int)v) {
357333
printf(" Fail checking defines: count %d v %d line %d\n",
358334
header_count,(int)v, __LINE__);
359335
exit(EXIT_FAILURE);
360336
}
361-
if (strlen(curdefname)+1 <= MAX_LNAME_LEN) {
337+
if (strlen(curdefname)+1 >= MAX_LNAME_LEN) {
362338
printf("Name %s is too long for table at %d\n",
363339
curdefname,
364340
(int)strlen(curdefname));
@@ -377,31 +353,6 @@ read_lname_hdr(char *path)
377353
fclose(fin);
378354
}
379355

380-
#if 0
381-
static void
382-
read_next_line(FILE *fin,unsigned linenum,unsigned int *quotedlen)
383-
{
384-
char *line = 0;
385-
size_t linelen = 0;
386-
unsigned int qlen2 = 0;
387-
388-
line = fgets(buffer2,MAXDEFINELINE,fin);
389-
if (!line) {
390-
printf("inner end file line %u is too long!\n",linenum);
391-
exit(EXIT_FAILURE);
392-
}
393-
linelen = strlen(line);
394-
line[linelen-1] = 0;
395-
--linelen;
396-
if (linelen >= (unsigned long)(MAXDEFINELINE-1)) {
397-
printf("inner line %u is too long!\n",linenum);
398-
exit(EXIT_FAILURE);
399-
}
400-
quoted_length(line,&qlen2);
401-
*quotedlen = qlen2;
402-
}
403-
404-
#endif
405356
static char pathbufhdr[2000];
406357
static char pathbufc[2000];
407358
static void
@@ -423,7 +374,6 @@ local_safe_strcpy(char *targ,char *src,
423374
int
424375
main(int argc, char **argv)
425376
{
426-
unsigned i = 0;
427377
char *path = 0;
428378
size_t len = 0;
429379
const char *libpath="/src/lib/libdwarf/dwarf.h";

0 commit comments

Comments
 (0)