Oh no, very sorry, that wasn't the problem with negative numbers... it
would just skip that digit. I wonder what is my problem then..

On Jun 14, 11:44 am, Parch <[EMAIL PROTECTED]> wrote:
> I had a bug today because Integer parsing wasn't detecting any errors
> while parsing a string with leading space: e.g. " -152". Once I
> realised that, I noticed that integer parsing for negative numbers is
> like totally broken as well, since it silently treats the '-' sign as
> a digit with value == radix...
>
> Suggesting a bug fix + a couple error detection changes to the code.
>
> Original Code:
>
> template <class T>
> static Integer StringToInteger(const T *str)
> {
>         int radix;
>         // GCC workaround
>         // std::char_traits<wchar_t>::length() not defined in GCC 3.2 and
> STLport 4.5.3
>         unsigned int length;
>         for (length = 0; str[length] != 0; length++) {}
>
>         Integer v;
>
>         if (length == 0)
>                 return v;
>
>         switch (str[length-1])
>         {
>         case 'h':
>         case 'H':
>                 radix=16;
>                 break;
>         case 'o':
>         case 'O':
>                 radix=8;
>                 break;
>         case 'b':
>         case 'B':
>                 radix=2;
>                 break;
>         default:
>                 radix=10;
>         }
>
>         if (length > 2 && str[0] == '0' && str[1] == 'x')
>                 radix = 16;
>
>         for (unsigned i=0; i<length; i++)
>         {
>                 int digit;
>
>                 if (str[i] >= '0' && str[i] <= '9')
>                         digit = str[i] - '0';
>                 else if (str[i] >= 'A' && str[i] <= 'F')
>                         digit = str[i] - 'A' + 10;
>                 else if (str[i] >= 'a' && str[i] <= 'f')
>                         digit = str[i] - 'a' + 10;
>                 else
>                         digit = radix;
>
>                 if (digit < radix)
>                 {
>                         v *= radix;
>                         v += digit;
>                 }
>         }
>
>         if (str[0] == '-')
>                 v.Negate();
>
>         return v;
>
> }
>
> Some changes:
>
> template <class T>
> static Integer StringToInteger(const T *str)
> {
>         int radix;
>         // GCC workaround
>         // std::char_traits<wchar_t>::length() not defined in GCC 3.2 and
> STLport 4.5.3
>         unsigned int length;
>         for (length = 0; str[length] != 0; length++) {}
>
>         Integer v;
>
>         //Should this really be allowed??
>         if (length == 0)
>                 return v;
>
>         switch (str[length-1])
>         {
>         case 'h':
>         case 'H':
>                 radix=16;
>                 length--; //don't parse the radix later
>                 break;
>         case 'o':
>         case 'O':
>                 radix=8;
>                 length--;
>                 break;
>         case 'b':
>         case 'B':
>                 radix=2;
>                 length--;
>                 break;
>         default:
>                 if( str[length-1] >= '0' && str[length-1] <= '9' )
>                         radix=10;
>                 else
>                         throw "Invalid radix in integer string 
> representation"; //something
> to this effect
>         }
>
>         if (length == 0)
>                 throw "Radix-only integer string representation illegal"; //
> something to this effect
>
>         if (length > 1 && str[0] == '-'){
>                 v.Negate();
>                 str++;
>                 length--;
>         }
>         else if (length > 2 && str[0] == '0' && str[1] == 'x'){
>                 radix = 16;
>                 str += 2;
>                 length -= 2;
>         }
>
>         for (unsigned i=0; i<length; i++)
>         {
>                 int digit;
>
>                 if (str[i] >= '0' && str[i] <= '9')
>                         digit = str[i] - '0';
>                 else if (str[i] >= 'A' && str[i] <= 'F')
>                         digit = str[i] - 'A' + 10;
>                 else if (str[i] >= 'a' && str[i] <= 'f')
>                         digit = str[i] - 'a' + 10;
>                 else
>                         digit = radix;
>
>                 if (digit < radix)
>                 {
>                         v *= radix;
>                         v += digit;
>                 }
>                 else throw "Invalid digit parsing integer from string"; 
> //something
> to this effect
>         }
>
>         return v;
>
> }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [EMAIL PROTECTED]
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
-~----------~----~----~----~------~----~------~--~---

Reply via email to