Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2005:
[Freeciv-Dev] (PR#12744) datafilelist shouldn't return const char **
Home

[Freeciv-Dev] (PR#12744) datafilelist shouldn't return const char **

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12744) datafilelist shouldn't return const char **
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 8 Apr 2005 08:55:56 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12744 >

The datafilelist function returns a "const char **".  But according to 
the function comment:

   The list is allocated when the function is called; it should either
   be stored permanently or de-allocated (by free'ing each element and
   the whole list).

In other words, the caller owns the return pointer, not the function. 
However it's impossible to "correctly" free the return values because of 
the const qualifier.  Basically, the const is wrong because since the 
caller owns the data it can do whatever it wants with the data.

This comes up in my tileset-preloading patch, which skips over some 
entries in the list and needs to free() them.

This patch removes the const qualifier.

-jason

Index: client/audio.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/audio.c,v
retrieving revision 1.20
diff -u -r1.20 audio.c
--- client/audio.c      6 Sep 2004 16:09:40 -0000       1.20
+++ client/audio.c      8 Apr 2005 15:51:14 -0000
@@ -88,7 +88,7 @@
   if (!audio_list) {
     /* Note: this means you must restart the client after installing a new
        soundset. */
-    audio_list = datafilelist(SNDSPEC_SUFFIX);
+    audio_list = (const char **)datafilelist(SNDSPEC_SUFFIX);
   }
 
   return audio_list;
Index: client/tilespec.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.c,v
retrieving revision 1.286
diff -u -r1.286 tilespec.c
--- client/tilespec.c   8 Apr 2005 06:32:27 -0000       1.286
+++ client/tilespec.c   8 Apr 2005 15:51:39 -0000
@@ -641,7 +641,7 @@
   if (!tileset_list) {
     /* Note: this means you must restart the client after installing a new
        tileset. */
-    tileset_list = datafilelist(TILESPEC_SUFFIX);
+    tileset_list = (const char **)datafilelist(TILESPEC_SUFFIX);
   }
 
   return tileset_list;
Index: server/gamehand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gamehand.c,v
retrieving revision 1.158
diff -u -r1.158 gamehand.c
--- server/gamehand.c   29 Mar 2005 12:28:26 -0000      1.158
+++ server/gamehand.c   8 Apr 2005 15:51:42 -0000
@@ -511,7 +511,7 @@
 static void send_ruleset_choices(struct connection *pc)
 {
   struct packet_ruleset_choices packet;
-  static const char **rulesets = NULL;
+  static char **rulesets = NULL;
   int i;
 
   if (pc->access_level != ALLOW_HACK) {
Index: utility/shared.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.c,v
retrieving revision 1.129
diff -u -r1.129 shared.c
--- utility/shared.c    23 Mar 2005 20:27:08 -0000      1.129
+++ utility/shared.c    8 Apr 2005 15:51:47 -0000
@@ -910,7 +910,7 @@
   The suffixes are removed from the filenames before the list is
   returned.
 ***************************************************************************/
-const char **datafilelist(const char* suffix)
+char **datafilelist(const char* suffix)
 {
   const char **dirs = get_data_dirs(NULL);
   char **file_list = NULL;
@@ -990,7 +990,7 @@
   file_list = fc_realloc(file_list, (num_matches + 1) * sizeof(*file_list));
   file_list[num_matches] = NULL;
 
-  return (const char **)file_list;
+  return file_list;
 }
 
 /***************************************************************************
Index: utility/shared.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.h,v
retrieving revision 1.142
diff -u -r1.142 shared.h
--- utility/shared.h    29 Mar 2005 12:28:27 -0000      1.142
+++ utility/shared.h    8 Apr 2005 15:51:47 -0000
@@ -225,7 +225,7 @@
                                                                                
 char *user_home_dir(void);
 const char *user_username(void);
-const char **datafilelist(const char *suffix);
+char **datafilelist(const char *suffix);
 struct datafile_list *datafilelist_infix(const char *subpath,
                                          const char *infix, bool nodups);
 char *datafilename(const char *filename);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12744) datafilelist shouldn't return const char **, Jason Short <=