Correctness first:

# man isdigit
ISALPHA(3)          Linux Programmer's Manual          ISALPHA(3)
NAME
       isalnum,  isalpha,  isascii,  isblank,  iscntrl,  isdigit,
       isgraph,  islower,  isprint,  ispunct,  isspace,  isupper,
       isxdigit - character classification routines
SYNOPSIS
       #include <ctype.h>
       int isalnum(int c);
       int isalpha(int c);
       int isascii(int c);
       int isblank(int c);
       int iscntrl(int c);
       int isdigit(int c);
       int isgraph(int c);
       int islower(int c);
       int isprint(int c);
       int ispunct(int c);
       int isspace(int c);
       int isupper(int c);
       int isxdigit(int c);
DESCRIPTION
       These functions check whether c, which must have the value
                                              ^^^^^^^^^^^^^^^^^^^
       of an unsigned char or EOF, falls into a certain character
       ^^^^^^^^^^^^^^^^^^^^^^^^^^
       class according to the current locale.

That is, if it so happens that char is signed, ctype.h functions
can give wrong results. This actually has bitten me once.
For clamav, isdigit happens to work correctly in current glibc,
but there is no guarantee it will do so forever.

Performance:
for(i=0; i<strlen(s); i++)... has quadratic run time.
Not a problem in current code (it is never called on
more than LINE_LENGTH (1024) bytes, thus touches 'only' 500 Kbytes at most),
but it is not a good practice anyway. Especially since we don't need
strlen() here _at all_.

Size: new code is twice as small. (Can be thrice as small if
glibc will implement isdigit() as if(c>='0' && c<='9') one day).

Run tested.
--
vda
--- clamav-0.84rc1/shared/cfgparser.c.orig	Mon Mar 21 03:13:46 2005
+++ clamav-0.84rc1/shared/cfgparser.c	Fri Apr 15 10:08:19 2005
@@ -33,12 +33,13 @@
 
 static int isnumb(const char *str)
 {
-	int i;
+	unsigned char c = *str;
 
-    for(i = 0; i < strlen(str); i++)
-	if(!isdigit(str[i]))
+    while(c) {
+	if(!isdigit(c))
 	    return 0;
-
+	c = *++str;
+    }
     return 1;
 }
 
_______________________________________________
http://lurker.clamav.net/list/clamav-devel.html

Reply via email to