[AD] Error in set_config_file() function with non existing file.

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


http://www.allegro.cc/forums/view_thread.php?_id=383591

As discovered by Apollo8, calling set_config_file before allegro_init
is dangerous because if the file doesn't exist, or it is a directory,
allegro will core. Test case:

   #include <allegro.h>
   
   int main(int argc, char *argv[])
   {
   	set_config_file("inexistant_file_or_directory.cfg");
   	allegro_init();
   	allegro_message("Works?\n");
   	set_config_string("system", "test", "ah");
   	return 0;
   }
   END_OF_MAIN()

The reason for the core is that some IO routine wants to set
allegro_errno, which is NULL, because allegro_init has not been
called yet. The following patch fixes this by avoiding any IO
operations if the system driver has not been initialised.

Index: src/config.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/config.c,v
retrieving revision 1.25
diff -u -r1.25 config.c
--- src/config.c        12 Aug 2003 17:49:23 -0000      1.25
+++ src/config.c        2 Aug 2004 18:57:47 -0000
@@ -446,6 +446,12 @@
       *config = NULL;
    }
 
+   /* Special case when allegro_init has not been called yet. */
+   if (!system_driver) {
+      set_config(config, NULL, 0, savefile);
+      return;
+   }
+   
    length = file_size(filename);
 
    if (length > 0) {

AFAICS this problem has been present in all 4.x versions, so I
would suggest porting this or any other bugfix to the stable branch
as well. Another possible way to deal with the error is to have
allegro_errno point at some internal static variable, which feels
more foolproof, at least for the release versions of the library.

OTOH, I would like to suggest adding assertions for system_driver
(there are already a few) to check for an uninitialised allegro
system. Since this would be a fairly usual check done for most
of the API, instead of a cryptic assert for newbies to Allegro's
internal, the assertion could be hidden behind a define like
VERIFY_ALLEGRO_IS_INITIALISED() or something like that.




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/