@@ -70,92 +70,122 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
7070 return 0 ;
7171}
7272
73- int parse_test_list ( const char * s ,
74- struct test_filter_set * set ,
75- bool is_glob_pattern )
73+ static int do_insert_test ( struct test_filter_set * set ,
74+ char * test_str ,
75+ char * subtest_str )
7676{
77- char * input , * state = NULL , * next ;
78- struct test_filter * tmp , * tests = NULL ;
79- int i , j , cnt = 0 ;
77+ struct test_filter * tmp , * test ;
78+ char * * ctmp ;
79+ int i ;
8080
81- input = strdup (s );
82- if (!input )
81+ for (i = 0 ; i < set -> cnt ; i ++ ) {
82+ test = & set -> tests [i ];
83+
84+ if (strcmp (test_str , test -> name ) == 0 ) {
85+ free (test_str );
86+ goto subtest ;
87+ }
88+ }
89+
90+ tmp = realloc (set -> tests , sizeof (* test ) * (set -> cnt + 1 ));
91+ if (!tmp )
8392 return - ENOMEM ;
8493
85- while ((next = strtok_r (state ? NULL : input , "," , & state ))) {
86- char * subtest_str = strchr (next , '/' );
87- char * pattern = NULL ;
88- int glob_chars = 0 ;
94+ set -> tests = tmp ;
95+ test = & set -> tests [set -> cnt ];
8996
90- tmp = realloc (tests , sizeof (* tests ) * (cnt + 1 ));
91- if (!tmp )
92- goto err ;
93- tests = tmp ;
97+ test -> name = test_str ;
98+ test -> subtests = NULL ;
99+ test -> subtest_cnt = 0 ;
94100
95- tests [cnt ].subtest_cnt = 0 ;
96- tests [cnt ].subtests = NULL ;
101+ set -> cnt ++ ;
97102
98- if (is_glob_pattern ) {
99- pattern = "%s" ;
100- } else {
101- pattern = "*%s*" ;
102- glob_chars = 2 ;
103- }
103+ subtest :
104+ if (!subtest_str )
105+ return 0 ;
104106
105- if (subtest_str ) {
106- char * * tmp_subtests = NULL ;
107- int subtest_cnt = tests [cnt ].subtest_cnt ;
108-
109- * subtest_str = '\0' ;
110- subtest_str += 1 ;
111- tmp_subtests = realloc (tests [cnt ].subtests ,
112- sizeof (* tmp_subtests ) *
113- (subtest_cnt + 1 ));
114- if (!tmp_subtests )
115- goto err ;
116- tests [cnt ].subtests = tmp_subtests ;
117-
118- tests [cnt ].subtests [subtest_cnt ] =
119- malloc (strlen (subtest_str ) + glob_chars + 1 );
120- if (!tests [cnt ].subtests [subtest_cnt ])
121- goto err ;
122- sprintf (tests [cnt ].subtests [subtest_cnt ],
123- pattern ,
124- subtest_str );
125-
126- tests [cnt ].subtest_cnt ++ ;
107+ for (i = 0 ; i < test -> subtest_cnt ; i ++ ) {
108+ if (strcmp (subtest_str , test -> subtests [i ]) == 0 ) {
109+ free (subtest_str );
110+ return 0 ;
127111 }
112+ }
128113
129- tests [cnt ].name = malloc (strlen (next ) + glob_chars + 1 );
130- if (!tests [cnt ].name )
131- goto err ;
132- sprintf (tests [cnt ].name , pattern , next );
114+ ctmp = realloc (test -> subtests ,
115+ sizeof (* test -> subtests ) * (test -> subtest_cnt + 1 ));
116+ if (!ctmp )
117+ return - ENOMEM ;
118+
119+ test -> subtests = ctmp ;
120+ test -> subtests [test -> subtest_cnt ] = subtest_str ;
133121
134- cnt ++ ;
122+ test -> subtest_cnt ++ ;
123+
124+ return 0 ;
125+ }
126+
127+ static int insert_test (struct test_filter_set * set ,
128+ char * test_spec ,
129+ bool is_glob_pattern )
130+ {
131+ char * pattern , * subtest_str , * ext_test_str , * ext_subtest_str = NULL ;
132+ int glob_chars = 0 ;
133+
134+ if (is_glob_pattern ) {
135+ pattern = "%s" ;
136+ } else {
137+ pattern = "*%s*" ;
138+ glob_chars = 2 ;
135139 }
136140
137- tmp = realloc (set -> tests , sizeof (* tests ) * (cnt + set -> cnt ));
138- if (!tmp )
141+ subtest_str = strchr (test_spec , '/' );
142+ if (subtest_str ) {
143+ * subtest_str = '\0' ;
144+ subtest_str += 1 ;
145+ }
146+
147+ ext_test_str = malloc (strlen (test_spec ) + glob_chars + 1 );
148+ if (!ext_test_str )
139149 goto err ;
140150
141- memcpy (tmp + set -> cnt , tests , sizeof (* tests ) * cnt );
142- set -> tests = tmp ;
143- set -> cnt += cnt ;
151+ sprintf (ext_test_str , pattern , test_spec );
144152
145- free (tests );
146- free (input );
147- return 0 ;
153+ if (subtest_str ) {
154+ ext_subtest_str = malloc (strlen (subtest_str ) + glob_chars + 1 );
155+ if (!ext_subtest_str )
156+ goto err ;
157+
158+ sprintf (ext_subtest_str , pattern , subtest_str );
159+ }
160+
161+ return do_insert_test (set , ext_test_str , ext_subtest_str );
148162
149163err :
150- for (i = 0 ; i < cnt ; i ++ ) {
151- for (j = 0 ; j < tests [i ].subtest_cnt ; j ++ )
152- free (tests [i ].subtests [j ]);
164+ free (ext_test_str );
165+ free (ext_subtest_str );
166+
167+ return - ENOMEM ;
168+ }
153169
154- free (tests [i ].name );
170+ int parse_test_list (const char * s ,
171+ struct test_filter_set * set ,
172+ bool is_glob_pattern )
173+ {
174+ char * input , * state = NULL , * test_spec ;
175+ int err = 0 ;
176+
177+ input = strdup (s );
178+ if (!input )
179+ return - ENOMEM ;
180+
181+ while ((test_spec = strtok_r (state ? NULL : input , "," , & state ))) {
182+ err = insert_test (set , test_spec , is_glob_pattern );
183+ if (err )
184+ break ;
155185 }
156- free ( tests );
186+
157187 free (input );
158- return - ENOMEM ;
188+ return err ;
159189}
160190
161191__u32 link_info_prog_id (const struct bpf_link * link , struct bpf_link_info * info )
0 commit comments