Forráskód Böngészése

conv: Fixed unsigned numbers conversion functions.

Previously the functions for unsigned numbers conversion convert negative
values to the absolute unsigned values. Now negative numbers are illegal
for such functions.

The test for the faux_conv_atoull() function is added.
Serj Kalichev 4 hónapja
szülő
commit
ef764f8c56
4 módosított fájl, 82 hozzáadás és 5 törlés
  1. 3 0
      faux/conv/Makefile.am
  2. 35 5
      faux/conv/conv.c
  3. 41 0
      faux/conv/testc_conv.c
  4. 3 0
      faux/testc_module/testc_module.c

+ 3 - 0
faux/conv/Makefile.am

@@ -1,3 +1,6 @@
 libfaux_la_SOURCES += \
 	faux/conv/conv.c
 
+if TESTC
+libfaux_la_SOURCES += faux/conv/testc_conv.c
+endif

+ 35 - 5
faux/conv/conv.c

@@ -55,9 +55,15 @@ bool_t faux_conv_atoul(const char *str, unsigned long int *val, int base)
 {
 	char *endptr = NULL;
 	unsigned long int res = 0;
+	const char *begin = str;
+
+	while (*begin == ' ')
+		begin++;
+	if (*begin == '-') // negative number is illegal
+		return BOOL_FALSE;
 
 	errno = 0; // man recommends to do so
-	res = strtoul(str, &endptr, base);
+	res = strtoul(begin, &endptr, base);
 	// Check for overflow
 	if ((ULONG_MAX == res) && (ERANGE == errno))
 		return BOOL_FALSE;
@@ -115,9 +121,15 @@ bool_t faux_conv_atoull(const char *str, unsigned long long int *val, int base)
 {
 	char *endptr = NULL;
 	unsigned long long int res = 0;
+	const char *begin = str;
+
+	while (*begin == ' ')
+		begin++;
+	if (*begin == '-') // negative number is illegal
+		return BOOL_FALSE;
 
 	errno = 0; // man recommends to do so
-	res = strtoull(str, &endptr, base);
+	res = strtoull(begin, &endptr, base);
 	// Check for overflow
 	if ((ULLONG_MAX == res) && (ERANGE == errno))
 		return BOOL_FALSE;
@@ -170,9 +182,15 @@ bool_t faux_conv_atoi(const char *str, int *val, int base)
 bool_t faux_conv_atoui(const char *str, unsigned int *val, int base)
 {
 	unsigned long int tmp = 0;
+	const char *begin = str;
+
+	while (*begin == ' ')
+		begin++;
+	if (*begin == '-') // negative number is illegal
+		return BOOL_FALSE;
 
 	// Use existent func. The long int is longer or equal to int.
-	if (!faux_conv_atoul(str, &tmp, base))
+	if (!faux_conv_atoul(begin, &tmp, base))
 		return BOOL_FALSE;
 	if (tmp > UINT_MAX) // Overflow
 		return BOOL_FALSE;
@@ -221,8 +239,14 @@ bool_t faux_conv_atos(const char *str, short *val, int base)
 bool_t faux_conv_atous(const char *str, unsigned short *val, int base)
 {
 	unsigned long int tmp = 0;
+	const char *begin = str;
+
+	while (*begin == ' ')
+		begin++;
+	if (*begin == '-') // negative number is illegal
+		return BOOL_FALSE;
 
-	if (!faux_conv_atoul(str, &tmp, base))
+	if (!faux_conv_atoul(begin, &tmp, base))
 		return BOOL_FALSE;
 	if (tmp > USHRT_MAX) // Overflow
 		return BOOL_FALSE;
@@ -271,8 +295,14 @@ bool_t faux_conv_atoc(const char *str, char *val, int base)
 bool_t faux_conv_atouc(const char *str, unsigned char *val, int base)
 {
 	unsigned long int tmp = 0;
+	const char *begin = str;
+
+	while (*begin == ' ')
+		begin++;
+	if (*begin == '-') // negative number is illegal
+		return BOOL_FALSE;
 
-	if (!faux_conv_atoul(str, &tmp, base))
+	if (!faux_conv_atoul(begin, &tmp, base))
 		return BOOL_FALSE;
 	if (tmp > UCHAR_MAX) // Overflow
 		return BOOL_FALSE;

+ 41 - 0
faux/conv/testc_conv.c

@@ -0,0 +1,41 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "faux/conv.h"
+
+
+int testc_faux_conv_atoull(void)
+{
+	int retval = 0;
+	unsigned long long val = 0;
+
+	// Negative integer
+	if (faux_conv_atoull("-123", &val, 0)) {
+		printf("The negative integer is converted to unsigned value\n");
+		retval = -1;
+	}
+
+	// Unsigned integer
+	if (!faux_conv_atoull("  234123", &val, 0)) {
+		printf("Can't convert legal unsigned integer\n");
+		retval = -1;
+	}
+	if (val != 234123) {
+		printf("Illegal unsigned integer conversion\n");
+		retval = -1;
+	}
+
+	// Hex integer
+	if (!faux_conv_atoull(" 0x234123", &val, 0)) {
+		printf("Can't convert legal hex integer\n");
+		retval = -1;
+	}
+	if (val != 0x234123) {
+		printf("Illegal hex integer conversion\n");
+		retval = -1;
+	}
+
+	return retval;
+}
+

+ 3 - 0
faux/testc_module/testc_module.c

@@ -13,6 +13,9 @@ const char *testc_module[][2] = {
 	// base
 	{"testc_faux_filesize", "Get size of filesystem object"},
 
+	// conv
+	{"testc_faux_conv_atoull", "String to unsigned long long conversion"},
+
 	// str
 	{"testc_faux_str_nextword", "Find next word (quotation)"},
 	{"testc_faux_str_getline", "Get line from string"},