Fixed bug 3341 - SDL_sscanf() problem
authorSam Lantinga <slouken@libsdl.org>
Fri, 11 Aug 2017 19:36:12 -0700
changeset 1122710b07421903d
parent 11226 7f429e943ed3
child 11228 af4c3dc6b97f
Fixed bug 3341 - SDL_sscanf() problem

e_pluschauskas

Why does SDL_sscanf() always returns the number of format specifiers and doesn't implements standard C library behavior?
src/stdlib/SDL_string.c
     1.1 --- a/src/stdlib/SDL_string.c	Fri Aug 11 18:56:41 2017 -0700
     1.2 +++ b/src/stdlib/SDL_string.c	Fri Aug 11 19:36:12 2017 -0700
     1.3 @@ -78,7 +78,7 @@
     1.4          value += v;
     1.5          ++text;
     1.6      }
     1.7 -    if (valuep) {
     1.8 +    if (valuep && text > textstart) {
     1.9          if (negative && value) {
    1.10              *valuep = -value;
    1.11          } else {
    1.12 @@ -114,7 +114,7 @@
    1.13          value += v;
    1.14          ++text;
    1.15      }
    1.16 -    if (valuep) {
    1.17 +    if (valuep && text > textstart) {
    1.18          *valuep = value;
    1.19      }
    1.20      return (text - textstart);
    1.21 @@ -146,7 +146,7 @@
    1.22          value += v;
    1.23          ++text;
    1.24      }
    1.25 -    if (valuep) {
    1.26 +    if (valuep && text > textstart) {
    1.27          *valuep = value;
    1.28      }
    1.29      return (text - textstart);
    1.30 @@ -183,7 +183,7 @@
    1.31          value += v;
    1.32          ++text;
    1.33      }
    1.34 -    if (valuep) {
    1.35 +    if (valuep && text > textstart) {
    1.36          if (negative && value) {
    1.37              *valuep = -value;
    1.38          } else {
    1.39 @@ -219,7 +219,7 @@
    1.40          value += v;
    1.41          ++text;
    1.42      }
    1.43 -    if (valuep) {
    1.44 +    if (valuep && text > textstart) {
    1.45          *valuep = value;
    1.46      }
    1.47      return (text - textstart);
    1.48 @@ -251,7 +251,7 @@
    1.49              ++text;
    1.50          }
    1.51      }
    1.52 -    if (valuep) {
    1.53 +    if (valuep && text > textstart) {
    1.54          if (negative && value) {
    1.55              *valuep = -value;
    1.56          } else {
    1.57 @@ -1025,6 +1025,10 @@
    1.58  {
    1.59      int retval = 0;
    1.60  
    1.61 +    if (!text || !*text) {
    1.62 +        return -1;
    1.63 +    }
    1.64 +
    1.65      while (*fmt) {
    1.66          if (*fmt == ' ') {
    1.67              while (SDL_isspace((unsigned char) *text)) {
    1.68 @@ -1044,6 +1048,7 @@
    1.69                  DO_LONG,
    1.70                  DO_LONGLONG
    1.71              } inttype = DO_INT;
    1.72 +            size_t advance;
    1.73              SDL_bool suppress = SDL_FALSE;
    1.74  
    1.75              ++fmt;
    1.76 @@ -1131,8 +1136,9 @@
    1.77                          }
    1.78                      } else {
    1.79                          long value;
    1.80 -                        text += SDL_ScanLong(text, radix, &value);
    1.81 -                        if (!suppress) {
    1.82 +                        advance = SDL_ScanLong(text, radix, &value);
    1.83 +                        text += advance;
    1.84 +                        if (advance && !suppress) {
    1.85                              switch (inttype) {
    1.86                              case DO_SHORT:
    1.87                                  {
    1.88 @@ -1175,16 +1181,18 @@
    1.89                  case 'u':
    1.90                      if (inttype == DO_LONGLONG) {
    1.91                          Uint64 value;
    1.92 -                        text += SDL_ScanUnsignedLongLong(text, radix, &value);
    1.93 -                        if (!suppress) {
    1.94 +                        advance = SDL_ScanUnsignedLongLong(text, radix, &value);
    1.95 +                        text += advance;
    1.96 +                        if (advance && !suppress) {
    1.97                              Uint64 *valuep = va_arg(ap, Uint64 *);
    1.98                              *valuep = value;
    1.99                              ++retval;
   1.100                          }
   1.101                      } else {
   1.102                          unsigned long value;
   1.103 -                        text += SDL_ScanUnsignedLong(text, radix, &value);
   1.104 -                        if (!suppress) {
   1.105 +                        advance = SDL_ScanUnsignedLong(text, radix, &value);
   1.106 +                        text += advance;
   1.107 +                        if (advance && !suppress) {
   1.108                              switch (inttype) {
   1.109                              case DO_SHORT:
   1.110                                  {
   1.111 @@ -1216,8 +1224,9 @@
   1.112                  case 'p':
   1.113                      {
   1.114                          uintptr_t value;
   1.115 -                        text += SDL_ScanUintPtrT(text, 16, &value);
   1.116 -                        if (!suppress) {
   1.117 +                        advance = SDL_ScanUintPtrT(text, 16, &value);
   1.118 +                        text += advance;
   1.119 +                        if (advance && !suppress) {
   1.120                              void **valuep = va_arg(ap, void **);
   1.121                              *valuep = (void *) value;
   1.122                              ++retval;
   1.123 @@ -1228,8 +1237,9 @@
   1.124                  case 'f':
   1.125                      {
   1.126                          double value;
   1.127 -                        text += SDL_ScanFloat(text, &value);
   1.128 -                        if (!suppress) {
   1.129 +                        advance = SDL_ScanFloat(text, &value);
   1.130 +                        text += advance;
   1.131 +                        if (advance && !suppress) {
   1.132                              float *valuep = va_arg(ap, float *);
   1.133                              *valuep = (float) value;
   1.134                              ++retval;