[PATCH:mkfontscale 2/2] Remove a couple of 'const' that aren't OK for the caller.

Thomas Klausner wiz at NetBSD.org
Sun Jun 30 04:21:08 PDT 2013


On Thu, Jun 06, 2013 at 10:32:26PM -0700, Alan Coopersmith wrote:
> On 06/ 2/13 12:16 PM, Thomas Klausner wrote:
> >---
> >  mkfontscale.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> >diff --git a/mkfontscale.c b/mkfontscale.c
> >index 53c5303..15efaac 100644
> >--- a/mkfontscale.c
> >+++ b/mkfontscale.c
> >@@ -60,7 +60,7 @@
> >  #define QUOTE(x)	#x
> >  #define STRINGIFY(x)	QUOTE(x)
> >
> >-static const char *encodings_array[] =
> >+static char *encodings_array[] =
> >      { "ascii-0",
> >        "iso8859-1", "iso8859-2", "iso8859-3", "iso8859-4", "iso8859-5",
> >        "iso8859-6", "iso8859-6.8", "iso8859-6.8x", "iso8859-6.16",
> >@@ -79,7 +79,7 @@ static const char *encodings_array[] =
> >        "gb2312.1980-0", "gb18030.2000-0", "gb18030.2000-1",
> >        "ksc5601.1987-0", "ksc5601.1992-3"};
> >
> >-static const char *extra_encodings_array[] =
> >+static char *extra_encodings_array[] =
> >      { "iso10646-1", "adobe-fontspecific", "microsoft-symbol" };
> >
> >  static ListPtr encodings, extra_encodings;
> >
> 
> I'm not sure what the best thing to do is here - removing the const just
> brings back all the warnings that we're using a non-const char * for all
> those string literals, but since we pass the array to
> 
> ListPtr makeList(char **a, int n, ListPtr old, int begin);
> 
> we get complaints about passing a const to a function wanting non-const.
> 
> I started looking at changing the List functions to all use const char *,
> but the xlfd lists pass dynamically allocated values to those and use
> deepDestroyList() to free them, so this code just expects some lists to
> have constant strings and some not to and to use the same functions &
> types for both, which is messy.

If I try keeping the const's, I come up with the attached, which as a
result of the problem you describe needs to cast a const char * to a
void * to allow it to be freed. Do you prefer this solution?
 Thomas
-------------- next part --------------
>From f556da0755ac72863b886a5a1f6f3eda3531f0c9 Mon Sep 17 00:00:00 2001
From: Thomas Klausner <wiz at NetBSD.org>
Date: Sun, 2 Jun 2013 21:16:39 +0200
Subject: [PATCH:mkfontscale] Add some const qualifiers.

Fixes clang warnings.

Signed-off-by: Thomas Klausner <wiz at NetBSD.org>
---
 list.c        |  4 ++--
 list.h        |  4 ++--
 mkfontscale.c | 10 +++++-----
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/list.c b/list.c
index 1614d95..35b2f2a 100644
--- a/list.c
+++ b/list.c
@@ -170,7 +170,7 @@ appendList(ListPtr first, ListPtr second)
 }
 
 ListPtr
-makeList(char **a, int n, ListPtr old, int begin)
+makeList(const char **a, int n, ListPtr old, int begin)
 {
     ListPtr first, current, next;
     int i;
@@ -274,7 +274,7 @@ deepDestroyList(ListPtr old)
         return;
     while(old) {
         next = old->next;
-        free(old->value);
+        free((void *)old->value);
         free(old);
         old = next;
     }
diff --git a/list.h b/list.h
index c68a78d..897c51a 100644
--- a/list.h
+++ b/list.h
@@ -28,7 +28,7 @@
 char *dsprintf(const char *f, ...) _X_ATTRIBUTE_PRINTF(1,2);
 
 typedef struct _List {
-    char *value;
+    const char *value;
     struct _List *next;
 } ListRec, *ListPtr;
 
@@ -39,7 +39,7 @@ ListPtr listConsF(ListPtr cdr, const char *f, ...) _X_ATTRIBUTE_PRINTF(2,3);
 ListPtr listAdjoinF(ListPtr cdr, const char *f, ...) _X_ATTRIBUTE_PRINTF(2,3);
 int listLength(ListPtr list);
 ListPtr appendList(ListPtr first, ListPtr second);
-ListPtr makeList(char **a, int n, ListPtr old, int begin);
+ListPtr makeList(const char **a, int n, ListPtr old, int begin);
 ListPtr reverseList(ListPtr old);
 ListPtr sortList(ListPtr old);
 void destroyList(ListPtr old);
diff --git a/mkfontscale.c b/mkfontscale.c
index a67f283..0447263 100644
--- a/mkfontscale.c
+++ b/mkfontscale.c
@@ -91,8 +91,8 @@ static const char *outfilename;
 #define countof(_a) (sizeof(_a)/sizeof((_a)[0]))
 
 static int doDirectory(const char*, int, ListPtr);
-static int checkEncoding(FT_Face face, char *encoding_name);
-static int checkExtraEncoding(FT_Face face, char *encoding_name, int found);
+static int checkEncoding(FT_Face face, const char *encoding_name);
+static int checkExtraEncoding(FT_Face face, const char *encoding_name, int found);
 static int find_cmap(int type, int pid, int eid, FT_Face face);
 static const char* notice_foundry(const char *notice);
 static const char* vendor_foundry(const signed char *vendor);
@@ -176,7 +176,7 @@ main(int argc, char **argv)
                 usage();
                 exit(1);
             }
-            makeList(&argv[argn + 1], 1, encodings, 0);
+            makeList((const char **)&argv[argn + 1], 1, encodings, 0);
             argn += 2;
         } else if(strcmp(argv[argn], "-p") == 0) {
             if(argn >= argc - 1) {
@@ -1019,7 +1019,7 @@ doDirectory(const char *dirname_given, int numEncodings, ListPtr encodingsToDo)
                          (c) == 0xAD || (c) == 0xF71B)
 
 static int
-checkEncoding(FT_Face face, char *encoding_name)
+checkEncoding(FT_Face face, const char *encoding_name)
 {
     FontEncPtr encoding;
     FontMapPtr mapping;
@@ -1191,7 +1191,7 @@ find_cmap(int type, int pid, int eid, FT_Face face)
 }
 
 static int
-checkExtraEncoding(FT_Face face, char *encoding_name, int found)
+checkExtraEncoding(FT_Face face, const char *encoding_name, int found)
 {
     int c;
 
-- 
1.8.3.1



More information about the xorg-devel mailing list