Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2004:
[Freeciv-Dev] (PR#9639) change is_iso_latin1() to is_ascii()
Home

[Freeciv-Dev] (PR#9639) change is_iso_latin1() to is_ascii()

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9639) change is_iso_latin1() to is_ascii()
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 8 Aug 2004 17:30:51 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9639 >

In shared.c there is a function is_iso_latin1 that is used as a helper 
to is_sane_name.  Obviously this assumes that the character given is in 
latin1.

Under PR#1824 the charset need not be latin1.  Often it will be latin1 
or UTF-8.  Or it might be latin2 or any other latin.  Or it could be 
just about anything.  The only restriction imposed (under the simplified 
design) is that it must be a superset of ascii.

Thus it makes sense to limit names to only ascii characters, which this 
patch does.

It might make further sense to limit names only to alphanumeric 
characters plus _.  However the danger is that in single-player mode the 
username is taken from the user's username on the machine.  We don't 
want this to turn out invalid.  Perhaps we could make a special-case for 
this (allowing the current username).  But more likely pubserver should 
just use a more specialized patch to restrict the name.

Another possibility I thought of was that there could be a string of 
allowed characters in names.  Normally this would just be a lengthy 
ascii list.  However it would be translatable so translators could 
change this for their language.  However this wouldn't work for 
pubserver (or any multi-lingual multiplayer games) and it distinguishes 
based on language not charset (which is okay but not ideal).  However 
this is still probably better than what's there now.  It's also more 
secure (according to the rule that you deny everything and then make 
exceptions, rather than including everything and make exceptions to that).

jason

Index: utility/shared.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/shared.c,v
retrieving revision 1.115
diff -u -r1.115 shared.c
--- utility/shared.c    20 Jul 2004 14:34:32 -0000      1.115
+++ utility/shared.c    9 Aug 2004 00:23:46 -0000
@@ -351,22 +351,14 @@
   return big_int_to_text(number, 0);
 }
 
-/***************************************************************
-  Check whether or not the given char is a valid,
-  printable ISO 8859-1 character.
-***************************************************************/
-static bool is_iso_latin1(char ch)
+/****************************************************************************
+  Check whether or not the given char is a valid ascii character.  The
+  character can be in any charset so long as it is a superset of ascii.
+****************************************************************************/
+static bool is_ascii(char ch)
 {
-   int i=ch;
-   
-  /* this works with both signed and unsignd char */
-  if (i>=0) {
-    if (ch < ' ')  return FALSE;
-    if (ch <= '~')  return TRUE;
-  }
-  if (ch < '¡')  return FALSE; /* FIXME: Is it really a good idea to
-                                use 8 bit characters in source code? */
-  return TRUE;
+  /* this works with both signed and unsigned char's. */
+  return ch >= ' ' && ch <= '~';
 }
 
 /***************************************************************
@@ -377,7 +369,8 @@
 ***************************************************************/
 bool is_sane_name(const char *name)
 {
-  const char *cp;
+  const char illegal_chars[] = {'|', '%', '"', ',', '*', '\0'};
+  int i, j;
 
   /* must not be NULL or empty */
   if (!name || *name == '\0') {
@@ -389,14 +382,17 @@
     return FALSE;
   }
 
-  /* must be composed entirely of printable ISO 8859-1 characters,
-   * and no illegal characters which can break ranking scripts */
-  for (cp = name; is_iso_latin1(*cp) && *cp != '|' && *cp != '%' 
-       && *cp != '"' && *cp != ',' && *cp != '*'; cp++) {
-    /* nothing */
-  }
-  if (*cp != '\0') {
-    return FALSE; 
+  /* must be composed entirely of printable ascii characters,
+   * and no illegal characters which can break ranking scripts. */
+  for (i = 0; name[i]; i++) {
+    if (!is_ascii(name[i])) {
+      return FALSE;
+    }
+    for (j = 0; illegal_chars[j]; j++) {
+      if (name[i] == illegal_chars[j]) {
+       return FALSE;
+      }
+    }
   }
 
   /* otherwise, it's okay... */

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#9639) change is_iso_latin1() to is_ascii(), Jason Short <=