[DRBD-cvs] svn commit by phil - r3010 - branches/drbd-8.0/user - Using the test isdigit(val1[0]) to find out if a string

drbd-cvs at lists.linbit.com drbd-cvs at lists.linbit.com
Thu Aug 2 21:18:00 CEST 2007


Author: phil
Date: 2007-08-02 21:17:57 +0200 (Thu, 02 Aug 2007)
New Revision: 3010

Modified:
   branches/drbd-8.0/user/drbdadm_adjust.c
   branches/drbd-8.0/user/drbdtool_common.c
   branches/drbd-8.0/user/drbdtool_common.h
Log:
Using the test isdigit(val1[0]) to find out if a string
represents a number is of course right out stupid and wrong.

When you used a shared secret that started with a digit,
this broke the "drbdadm adjust" function.

Fixed now.


Modified: branches/drbd-8.0/user/drbdadm_adjust.c
===================================================================
--- branches/drbd-8.0/user/drbdadm_adjust.c	2007-08-02 14:17:20 UTC (rev 3009)
+++ branches/drbd-8.0/user/drbdadm_adjust.c	2007-08-02 19:17:57 UTC (rev 3010)
@@ -29,7 +29,6 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
-#include <ctype.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -77,13 +76,9 @@
 	if(val1 == NULL && val2 == NULL) return 1;
 	if(val1 == NULL || val2 == NULL) return 0;
 
-	if(isdigit(val1[0])) {
-		v1 = m_strtoll(val1,0);
-		v2 = m_strtoll(val2,0);
+	if(new_strtoll(val1,0,&v1) == MSE_OK &&
+	   new_strtoll(val2,0,&v2) == MSE_OK) return v1 == v2;
 
-		return v1 == v2;
-	}
-
 	return !strcmp(val1,val2);
 }
 

Modified: branches/drbd-8.0/user/drbdtool_common.c
===================================================================
--- branches/drbd-8.0/user/drbdtool_common.c	2007-08-02 14:17:20 UTC (rev 3009)
+++ branches/drbd-8.0/user/drbdtool_common.c	2007-08-02 19:17:57 UTC (rev 3010)
@@ -53,88 +53,96 @@
   return buffer;
 }
 
-unsigned long long
-m_strtoll(const char *s, const char def_unit)
+int
+new_strtoll(const char *s, const char def_unit, unsigned long long *rv)
 {
-  unsigned long long r;
-  char unit = 0;
-  char dummy = 0;
-  int shift, c;
+	char unit = 0;
+	char dummy = 0;
+	int shift, c;
 
-  /*
-   * paranoia
-   */
-  switch (def_unit)
-    {
-    default:
-      fprintf(stderr, "%s:%d: unexpected default unit\n", __FILE__, __LINE__);
-      exit(100);
-    case 0:
-    case 1:
-    case '1':
-      shift = 0;
-      break;
+	switch (def_unit) {
+	default:
+		return MSE_DEFAULT_UNIT;
+	case 0:
+	case 1:
+	case '1':
+		shift = 0;
+		break;
+	case 'K':
+	case 'k':
+		shift = -10;
+		break;
+	case 's':
+		shift = -9;   // sectors
+		break;
+		/*
+		  case 'M':
+		  case 'm':
+		  case 'G':
+		  case 'g':
+		*/
+	}
+	
+	if (!s || !*s) return MSE_MISSING_NUMBER;
 
-    case 'K':
-    case 'k':
-      shift = -10;
-      break;
+	c = sscanf(s, "%llu%c%c", rv, &unit, &dummy);
 
-    case 's':
-      shift = -9;   // sectors
-      break;
+	if (c != 1 && c != 2) return MSE_INVALID_NUMBER;
 
-      /*
-         case 'M':
-         case 'm':
-         case 'G':
-         case 'g':
-       */
-    }
+	switch (unit) {
+	case 0:
+		return MSE_OK;
+	case 'K':
+	case 'k':
+		shift += 10;
+		break;
+	case 'M':
+	case 'm':
+		shift += 20;
+		break;
+	case 'G':
+	case 'g':
+		shift += 30;
+		break;
+	case 's':
+		shift += 9;
+		break;		
+	default:
+		return MSE_INVALID_UNIT;
+	}
+	if (*rv > (~0ULL >> shift)) return MSE_OUT_OF_RANGE;
 
-  if (!s || !*s)
-    {
-      fprintf(stderr, "missing number argument\n");
-      exit(100);
-    }
+	*rv = *rv << shift;
+	return MSE_OK;
+}
 
-  c = sscanf(s, "%llu%c%c", &r, &unit, &dummy);
+unsigned long long
+m_strtoll(const char *s, const char def_unit)
+{
+	unsigned long long r;
 
-  if (c != 1 && c != 2)
-    {
-      fprintf(stderr, "%s is not a valid number\n", s);
-      exit(20);
-    }
-
-  switch (unit)
-    {
-    case 0:
-      return r;
-    case 'K':
-    case 'k':
-      shift += 10;
-      break;
-    case 'M':
-    case 'm':
-      shift += 20;
-      break;
-    case 'G':
-    case 'g':
-      shift += 30;
-      break;
-    case 's':
-      shift += 9;
-      break;
-    default:
-      fprintf(stderr, "%s is not a valid number\n", s);
-      exit(20);
-    }
-  if (r > (~0ULL >> shift))
-    {
-      fprintf(stderr, "%s: out of range\n", s);
-      exit(20);
-    }
-  return r << shift;
+	switch(new_strtoll(s, def_unit, &r)) {
+	case MSE_OK:
+		return r;
+	case MSE_DEFAULT_UNIT:
+		fprintf(stderr, "unexpected default unit: %d\n",def_unit);
+		exit(100);
+	case MSE_MISSING_NUMBER:
+		fprintf(stderr, "missing number argument\n");
+		exit(100);
+	case MSE_INVALID_NUMBER:
+		fprintf(stderr, "%s is not a valid number\n", s);
+		exit(20);
+	case MSE_INVALID_UNIT:
+		fprintf(stderr, "%s is not a valid number\n", s);
+		exit(20);
+	case MSE_OUT_OF_RANGE:
+		fprintf(stderr, "%s: out of range\n", s);
+		exit(20);
+	default:
+		fprintf(stderr, "m_stroll() is confused\n");
+		exit(20);
+	}
 }
 
 void alarm_handler(int __attribute((unused)) signo)

Modified: branches/drbd-8.0/user/drbdtool_common.h
===================================================================
--- branches/drbd-8.0/user/drbdtool_common.h	2007-08-02 14:17:20 UTC (rev 3009)
+++ branches/drbd-8.0/user/drbdtool_common.h	2007-08-02 19:17:57 UTC (rev 3010)
@@ -23,12 +23,22 @@
 */
 #define PERROR(fmt, args...) fprintf(stderr, fmt ": %m\n" , ##args);
 
+enum new_strtoll_errs {
+	MSE_OK,
+	MSE_DEFAULT_UNIT,
+	MSE_MISSING_NUMBER,
+	MSE_INVALID_NUMBER,
+	MSE_INVALID_UNIT,
+	MSE_OUT_OF_RANGE,
+};
+
 struct option;
 
 extern int dt_lock_drbd(const char* device);
 extern void dt_unlock_drbd(int lock_fd);
 extern void dt_release_lockfile(int drbd_fd);
 extern int dt_minor_of_dev(const char *device);
+extern int new_strtoll(const char *s, const char def_unit, unsigned long long *rv);
 extern unsigned long long m_strtoll(const char* s,const char def_unit);
 extern const char* make_optstring(struct option *options, char startc);
 extern char* ppsize(char* buf, size_t size);



More information about the drbd-cvs mailing list