@@ -124,35 +124,45 @@ store_virtualpkg(struct xbps_handle *xhp, const char *path, size_t line, char *v
124124 return 0 ;
125125}
126126
127- static void
127+ static int
128128store_preserved_file (struct xbps_handle * xhp , const char * file )
129129{
130+ char path [PATH_MAX ];
130131 glob_t globbuf ;
131- char * p = NULL , * rfile = NULL ;
132+ char * p = NULL ;
132133 size_t len ;
133- int rv = 0 ;
134+ int r ;
134135
135- if (xhp -> preserved_files == NULL ) {
136+ if (! xhp -> preserved_files ) {
136137 xhp -> preserved_files = xbps_array_create ();
137- assert (xhp -> preserved_files );
138+ if (!xhp -> preserved_files )
139+ return xbps_error_oom ();
138140 }
139141
140- rfile = xbps_xasprintf ("%s%s" , xhp -> rootdir , file );
142+ if (xbps_path_join (path , sizeof (path ), xhp -> rootdir , file , (char * )NULL ) == -1 )
143+ return - errno ;
141144
142- rv = glob (rfile , 0 , NULL , & globbuf );
143- if (rv == GLOB_NOMATCH ) {
145+ r = glob (path , 0 , NULL , & globbuf );
146+ if (r == GLOB_NOMATCH ) {
144147 if (xbps_match_string_in_array (xhp -> preserved_files , file ))
145148 goto out ;
146149 xbps_array_add_cstring (xhp -> preserved_files , file );
147150 xbps_dbg_printf ("Added preserved file: %s\n" , file );
151+ r = 0 ;
152+ goto out ;
153+ } else if (r == GLOB_NOSPACE ) {
154+ r = xbps_error_oom ();
148155 goto out ;
149- } else if (rv != 0 ) {
156+ } else if (r != 0 ) {
157+ r = xbps_error_errno (errno , "glob failed: %s\n" , strerror (errno ));
150158 goto out ;
151159 }
160+ r = 0 ;
152161 for (size_t i = 0 ; i < globbuf .gl_pathc ; i ++ ) {
153162 if (xbps_match_string_in_array (xhp -> preserved_files , globbuf .gl_pathv [i ]))
154163 continue ;
155164
165+ // XXX: clean this up
156166 len = strlen (globbuf .gl_pathv [i ]) - strlen (xhp -> rootdir ) + 1 ;
157167 p = malloc (len );
158168 assert (p );
@@ -163,7 +173,7 @@ store_preserved_file(struct xbps_handle *xhp, const char *file)
163173 }
164174out :
165175 globfree (& globbuf );
166- free ( rfile ) ;
176+ return r ;
167177}
168178
169179static bool
@@ -175,28 +185,34 @@ store_repo(struct xbps_handle *xhp, const char *repo)
175185 return xbps_repo_store (xhp , repo );
176186}
177187
178- static void
188+ static int
179189store_ignored_pkg (struct xbps_handle * xhp , const char * pkgname )
180190{
181- if (xhp -> ignored_pkgs == NULL ) {
191+ if (! xhp -> ignored_pkgs ) {
182192 xhp -> ignored_pkgs = xbps_array_create ();
183- assert (xhp -> ignored_pkgs );
193+ if (!xhp -> ignored_pkgs )
194+ return xbps_error_oom ();
184195 }
185- xbps_array_add_cstring (xhp -> ignored_pkgs , pkgname );
196+ if (!xbps_array_add_cstring (xhp -> ignored_pkgs , pkgname ))
197+ return xbps_error_oom ();
186198 xbps_dbg_printf ("Added ignored package: %s\n" , pkgname );
199+ return 0 ;
187200}
188201
189- static void
202+ static int
190203store_noextract (struct xbps_handle * xhp , const char * value )
191204{
192205 if (* value == '\0' )
193- return ;
194- if (xhp -> noextract == NULL ) {
206+ return 0 ;
207+ if (! xhp -> noextract ) {
195208 xhp -> noextract = xbps_array_create ();
196- assert (xhp -> noextract );
209+ if (!xhp -> noextract )
210+ return xbps_error_oom ();
197211 }
198- xbps_array_add_cstring (xhp -> noextract , value );
212+ if (!xbps_array_add_cstring (xhp -> noextract , value ))
213+ return xbps_error_oom ();
199214 xbps_dbg_printf ("Added noextract pattern: %s\n" , value );
215+ return 0 ;
200216}
201217
202218enum {
@@ -291,20 +307,23 @@ static int
291307parse_files_glob (struct xbps_handle * xhp , xbps_dictionary_t seen ,
292308 const char * cwd , const char * pat , bool nested )
293309{
294- char tmppath [PATH_MAX ];
310+ char path [PATH_MAX ];
295311 glob_t globbuf ;
296- int rs , rv = 0 , rv2 ;
297-
298- rs = snprintf (tmppath , PATH_MAX , "%s/%s" ,
299- pat [0 ] == '/' ? xhp -> rootdir : cwd , pat );
300- if (rs < 0 || rs >= PATH_MAX )
301- return ENOMEM ;
302-
303- switch (glob (tmppath , 0 , NULL , & globbuf )) {
304- case 0 : break ;
305- case GLOB_NOSPACE : return ENOMEM ;
306- case GLOB_NOMATCH : return 0 ;
307- default : return 0 ;
312+ int r = 0 ;
313+
314+ if (xbps_path_join (path , sizeof (path ),
315+ pat [0 ] == '/' ? xhp -> rootdir : cwd , pat , (char * )NULL ) == -1 )
316+ return - ENAMETOOLONG ;
317+
318+ switch (glob (path , 0 , NULL , & globbuf )) {
319+ case 0 :
320+ break ;
321+ case GLOB_NOSPACE :
322+ r = xbps_error_oom ();
323+ goto out ;
324+ case GLOB_NOMATCH :
325+ default :
326+ goto out ;
308327 }
309328 for (size_t i = 0 ; i < globbuf .gl_pathc ; i ++ ) {
310329 if (seen != NULL ) {
@@ -313,14 +332,28 @@ parse_files_glob(struct xbps_handle *xhp, xbps_dictionary_t seen,
313332 fname = basename (globbuf .gl_pathv [i ]);
314333 if (xbps_dictionary_get_bool (seen , fname , & mask ) && mask )
315334 continue ;
316- xbps_dictionary_set_bool (seen , fname , true);
335+ if (!xbps_dictionary_set_bool (seen , fname , true)) {
336+ r = xbps_error_oom ();
337+ goto out ;
338+ }
317339 }
318- if ((rv2 = parse_file (xhp , globbuf .gl_pathv [i ], nested )) != 0 )
319- rv = rv2 ;
340+ r = parse_file (xhp , globbuf .gl_pathv [i ], nested );
341+ if (r < 0 )
342+ goto out ;
320343 }
344+ r = 0 ;
345+ out :
321346 globfree (& globbuf );
347+ return r ;
348+ }
322349
323- return rv ;
350+ static int
351+ store_string (char * dst , size_t dstsz , const char * src )
352+ {
353+ size_t n = strlcpy (dst , src , dstsz );
354+ if (n >= dstsz )
355+ return - ENOBUFS ;
356+ return 0 ;
324357}
325358
326359static int
@@ -330,14 +363,14 @@ parse_file(struct xbps_handle *xhp, const char *path, bool nested)
330363 size_t len , nlines = 0 ;
331364 ssize_t rd ;
332365 char * linebuf = NULL ;
333- int rv = 0 ;
334- int size , rs ;
366+ int r = 0 ;
335367 char * dir ;
336368
337- if ((fp = fopen (path , "r" )) == NULL ) {
338- rv = errno ;
339- xbps_error_printf ("cannot read configuration file %s: %s\n" , path , strerror (rv ));
340- return rv ;
369+ fp = fopen (path , "r" );
370+ if (!fp ) {
371+ return xbps_error_errno (errno ,
372+ "cannot read configuration file %s: %s\n" , path ,
373+ strerror (errno ));
341374 }
342375
343376 xbps_dbg_printf ("Parsing configuration file: %s\n" , path );
@@ -367,30 +400,21 @@ parse_file(struct xbps_handle *xhp, const char *path, bool nested)
367400 "line %zu\n" , path , nlines );
368401 continue ;
369402 case KEY_ROOTDIR :
370- size = sizeof xhp -> rootdir ;
371- rs = snprintf (xhp -> rootdir , size , "%s" , val );
372- if (rs < 0 || rs >= size ) {
373- rv = ENOMEM ;
403+ r = store_string (xhp -> rootdir , sizeof (xhp -> rootdir ), val );
404+ if (r < 0 )
374405 break ;
375- }
376406 xbps_dbg_printf ("%s: rootdir set to %s\n" , path , val );
377407 break ;
378408 case KEY_CACHEDIR :
379- size = sizeof xhp -> cachedir ;
380- rs = snprintf (xhp -> cachedir , size , "%s" , val );
381- if (rs < 0 || rs >= size ) {
382- rv = ENOMEM ;
409+ r = store_string (xhp -> cachedir , sizeof (xhp -> cachedir ), val );
410+ if (r < 0 )
383411 break ;
384- }
385412 xbps_dbg_printf ("%s: cachedir set to %s\n" , path , val );
386413 break ;
387414 case KEY_ARCHITECTURE :
388- size = sizeof xhp -> native_arch ;
389- rs = snprintf (xhp -> native_arch , size , "%s" , val );
390- if (rs < 0 || rs >= size ) {
391- rv = ENOMEM ;
415+ r = store_string (xhp -> native_arch , sizeof (xhp -> native_arch ), val );
416+ if (r < 0 )
392417 break ;
393- }
394418 xbps_dbg_printf ("%s: native architecture set to %s\n" , path ,
395419 val );
396420 break ;
@@ -417,15 +441,10 @@ parse_file(struct xbps_handle *xhp, const char *path, bool nested)
417441 xbps_dbg_printf ("%s: added repository %s\n" , path , val );
418442 break ;
419443 case KEY_VIRTUALPKG :
420- rv = store_virtualpkg (xhp , path , nlines , val );
421- if (rv < 0 ) {
422- rv = - rv ;
423- break ;
424- }
425- rv = 0 ;
444+ r = store_virtualpkg (xhp , path , nlines , val );
426445 break ;
427446 case KEY_PRESERVE :
428- store_preserved_file (xhp , val );
447+ r = store_preserved_file (xhp , val );
429448 break ;
430449 case KEY_KEEPCONF :
431450 if (strcasecmp (val , "true" ) == 0 ) {
@@ -446,10 +465,10 @@ parse_file(struct xbps_handle *xhp, const char *path, bool nested)
446465 }
447466 break ;
448467 case KEY_IGNOREPKG :
449- store_ignored_pkg (xhp , val );
468+ r = store_ignored_pkg (xhp , val );
450469 break ;
451470 case KEY_NOEXTRACT :
452- store_noextract (xhp , val );
471+ r = store_noextract (xhp , val );
453472 break ;
454473 case KEY_INCLUDE :
455474 /* Avoid double-nested parsing, only allow it once */
@@ -458,39 +477,42 @@ parse_file(struct xbps_handle *xhp, const char *path, bool nested)
458477 continue ;
459478 }
460479 dir = strdup (path );
461- rv = parse_files_glob (xhp , NULL , dirname (dir ), val , true);
480+ r = parse_files_glob (xhp , NULL , dirname (dir ), val , true);
462481 free (dir );
463482 break ;
464483 }
465484 }
466485 free (linebuf );
467486 fclose (fp );
468487
469- return rv ;
488+ return r ;
470489}
471490
472491int HIDDEN
473492xbps_conf_init (struct xbps_handle * xhp )
474493{
475494 xbps_dictionary_t seen ;
476- int rv = 0 ;
495+ int r = 0 ;
477496
478497 assert (xhp );
479498 seen = xbps_dictionary_create ();
480499 assert (seen );
481500
482- if (* xhp -> confdir ) {
501+ if (xhp -> confdir [ 0 ] ) {
483502 xbps_dbg_printf ("Processing configuration directory: %s\n" , xhp -> confdir );
484- if ((rv = parse_files_glob (xhp , seen , xhp -> confdir , "*.conf" , false)))
503+ r = parse_files_glob (xhp , seen , xhp -> confdir , "*.conf" , false);
504+ if (r < 0 )
485505 goto out ;
486506 }
487- if (* xhp -> sysconfdir ) {
507+ if (xhp -> sysconfdir [ 0 ] ) {
488508 xbps_dbg_printf ("Processing system configuration directory: %s\n" , xhp -> sysconfdir );
489- if ((rv = parse_files_glob (xhp , seen , xhp -> sysconfdir , "*.conf" , false)))
509+ r = parse_files_glob (
510+ xhp , seen , xhp -> sysconfdir , "*.conf" , false);
511+ if (r < 0 )
490512 goto out ;
491513 }
492514
493515out :
494516 xbps_object_release (seen );
495- return rv ;
517+ return r ;
496518}
0 commit comments