Fixed bug 3760 - RWops doesn't check for integer overflow when stdio_fseek only supports 32 bits
authorSam Lantinga <slouken@libsdl.org>
Sat, 09 Sep 2017 08:36:37 -0700
changeset 11480e7a79b236dc0
parent 11479 74650172be10
child 11481 490c36e2945d
Fixed bug 3760 - RWops doesn't check for integer overflow when stdio_fseek only supports 32 bits

Simon Hug

When RWops seeks with fseek or fseeko it uses the types long or off_t which can be 32 bits on some platforms. stdio_seek does not check if the 64-bit integer for the offset fits into a 32-bit integer. Offsets equal or larger than 2 GiB will have implementation-defined behavior and failure states would be very confusing to debug.

The attached patch adds range checking by using the macros from limits.h for long type and some bit shifting for off_t because POSIX couldn't be bothered to specify min and max macros.

It also defines HAVE_FSEEKI64 in SDL_config_windows.h so that the Windows function gets picked up automatically with the default config.

And there's an additional error message for when ftell fails.
CMakeLists.txt
configure
configure.in
include/SDL_config.h.cmake
include/SDL_config.h.in
include/SDL_config_android.h
include/SDL_config_iphoneos.h
include/SDL_config_macosx.h
include/SDL_config_pandora.h
include/SDL_config_psp.h
include/SDL_config_windows.h
include/SDL_config_winrt.h
include/SDL_config_wiz.h
src/file/SDL_rwops.c
     1.1 --- a/CMakeLists.txt	Sat Sep 09 08:34:46 2017 -0700
     1.2 +++ b/CMakeLists.txt	Sat Sep 09 08:36:37 2017 -0700
     1.3 @@ -622,7 +622,7 @@
     1.4  if(LIBC)
     1.5    if(WINDOWS AND NOT MINGW)
     1.6      set(HAVE_LIBC TRUE)
     1.7 -    foreach(_HEADER stdio.h string.h wchar.h ctype.h math.h)
     1.8 +    foreach(_HEADER stdio.h string.h wchar.h ctype.h math.h limits.h)
     1.9        string(TOUPPER "HAVE_${_HEADER}" _UPPER)
    1.10        string(REPLACE "." "_" _HAVE_H ${_UPPER})
    1.11        set(${_HAVE_H} 1)
    1.12 @@ -648,7 +648,7 @@
    1.13      set(HAVE_LIBC TRUE)
    1.14      check_include_file(sys/types.h HAVE_SYS_TYPES_H)
    1.15      foreach(_HEADER
    1.16 -            stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h
    1.17 +            stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h limits.h
    1.18              strings.h wchar.h inttypes.h stdint.h ctype.h math.h iconv.h signal.h)
    1.19        string(TOUPPER "HAVE_${_HEADER}" _UPPER)
    1.20        string(REPLACE "." "_" _HAVE_H ${_UPPER})
     2.1 --- a/configure	Sat Sep 09 08:34:46 2017 -0700
     2.2 +++ b/configure	Sat Sep 09 08:36:37 2017 -0700
     2.3 @@ -16171,7 +16171,7 @@
     2.4  
     2.5  fi
     2.6  
     2.7 -    for ac_header in sys/types.h stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h strings.h wchar.h inttypes.h stdint.h ctype.h math.h float.h iconv.h signal.h
     2.8 +    for ac_header in sys/types.h stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h strings.h wchar.h inttypes.h stdint.h limits.h ctype.h math.h float.h iconv.h signal.h
     2.9  do :
    2.10    as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
    2.11  ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
     3.1 --- a/configure.in	Sat Sep 09 08:34:46 2017 -0700
     3.2 +++ b/configure.in	Sat Sep 09 08:36:37 2017 -0700
     3.3 @@ -234,7 +234,7 @@
     3.4  
     3.5      dnl Check for C library headers
     3.6      AC_HEADER_STDC
     3.7 -    AC_CHECK_HEADERS(sys/types.h stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h strings.h wchar.h inttypes.h stdint.h ctype.h math.h float.h iconv.h signal.h)
     3.8 +    AC_CHECK_HEADERS(sys/types.h stdio.h stdlib.h stddef.h stdarg.h malloc.h memory.h string.h strings.h wchar.h inttypes.h stdint.h limits.h ctype.h math.h float.h iconv.h signal.h)
     3.9  
    3.10      dnl Check for typedefs, structures, etc.
    3.11      AC_TYPE_SIZE_T
     4.1 --- a/include/SDL_config.h.cmake	Sat Sep 09 08:34:46 2017 -0700
     4.2 +++ b/include/SDL_config.h.cmake	Sat Sep 09 08:36:37 2017 -0700
     4.3 @@ -52,24 +52,25 @@
     4.4  #if HAVE_LIBC
     4.5  
     4.6  /* Useful headers */
     4.7 +#cmakedefine STDC_HEADERS 1
     4.8  #cmakedefine HAVE_ALLOCA_H 1
     4.9 -#cmakedefine HAVE_SYS_TYPES_H 1
    4.10 -#cmakedefine HAVE_STDIO_H 1
    4.11 -#cmakedefine STDC_HEADERS 1
    4.12 -#cmakedefine HAVE_STDLIB_H 1
    4.13 -#cmakedefine HAVE_STDARG_H 1
    4.14 -#cmakedefine HAVE_MALLOC_H 1
    4.15 -#cmakedefine HAVE_MEMORY_H 1
    4.16 -#cmakedefine HAVE_STRING_H 1
    4.17 -#cmakedefine HAVE_STRINGS_H 1
    4.18 -#cmakedefine HAVE_WCHAR_H 1
    4.19 -#cmakedefine HAVE_INTTYPES_H 1
    4.20 -#cmakedefine HAVE_STDINT_H 1
    4.21  #cmakedefine HAVE_CTYPE_H 1
    4.22 -#cmakedefine HAVE_MATH_H 1
    4.23  #cmakedefine HAVE_FLOAT_H 1
    4.24  #cmakedefine HAVE_ICONV_H 1
    4.25 +#cmakedefine HAVE_INTTYPES_H 1
    4.26 +#cmakedefine HAVE_LIMITS_H 1
    4.27 +#cmakedefine HAVE_MALLOC_H 1
    4.28 +#cmakedefine HAVE_MATH_H 1
    4.29 +#cmakedefine HAVE_MEMORY_H 1
    4.30  #cmakedefine HAVE_SIGNAL_H 1
    4.31 +#cmakedefine HAVE_STDARG_H 1
    4.32 +#cmakedefine HAVE_STDINT_H 1
    4.33 +#cmakedefine HAVE_STDIO_H 1
    4.34 +#cmakedefine HAVE_STDLIB_H 1
    4.35 +#cmakedefine HAVE_STRINGS_H 1
    4.36 +#cmakedefine HAVE_STRING_H 1
    4.37 +#cmakedefine HAVE_SYS_TYPES_H 1
    4.38 +#cmakedefine HAVE_WCHAR_H 1
    4.39  #cmakedefine HAVE_PTHREAD_NP_H 1
    4.40  
    4.41  /* C library functions */
     5.1 --- a/include/SDL_config.h.in	Sat Sep 09 08:34:46 2017 -0700
     5.2 +++ b/include/SDL_config.h.in	Sat Sep 09 08:36:37 2017 -0700
     5.3 @@ -55,24 +55,25 @@
     5.4  #if HAVE_LIBC
     5.5  
     5.6  /* Useful headers */
     5.7 +#undef STDC_HEADERS
     5.8  #undef HAVE_ALLOCA_H
     5.9 -#undef HAVE_SYS_TYPES_H
    5.10 -#undef HAVE_STDIO_H
    5.11 -#undef STDC_HEADERS
    5.12 -#undef HAVE_STDLIB_H
    5.13 -#undef HAVE_STDARG_H
    5.14 -#undef HAVE_MALLOC_H
    5.15 -#undef HAVE_MEMORY_H
    5.16 -#undef HAVE_STRING_H
    5.17 -#undef HAVE_STRINGS_H
    5.18 -#undef HAVE_WCHAR_H
    5.19 -#undef HAVE_INTTYPES_H
    5.20 -#undef HAVE_STDINT_H
    5.21  #undef HAVE_CTYPE_H
    5.22 -#undef HAVE_MATH_H
    5.23  #undef HAVE_FLOAT_H
    5.24  #undef HAVE_ICONV_H
    5.25 +#undef HAVE_INTTYPES_H
    5.26 +#undef HAVE_LIMITS_H
    5.27 +#undef HAVE_MALLOC_H
    5.28 +#undef HAVE_MATH_H
    5.29 +#undef HAVE_MEMORY_H
    5.30  #undef HAVE_SIGNAL_H
    5.31 +#undef HAVE_STDARG_H
    5.32 +#undef HAVE_STDINT_H
    5.33 +#undef HAVE_STDIO_H
    5.34 +#undef HAVE_STDLIB_H
    5.35 +#undef HAVE_STRINGS_H
    5.36 +#undef HAVE_STRING_H
    5.37 +#undef HAVE_SYS_TYPES_H
    5.38 +#undef HAVE_WCHAR_H
    5.39  #undef HAVE_PTHREAD_NP_H
    5.40  
    5.41  /* C library functions */
     6.1 --- a/include/SDL_config_android.h	Sat Sep 09 08:34:46 2017 -0700
     6.2 +++ b/include/SDL_config_android.h	Sat Sep 09 08:36:37 2017 -0700
     6.3 @@ -35,16 +35,17 @@
     6.4  
     6.5  #define HAVE_GCC_ATOMICS    1
     6.6  
     6.7 +#define STDC_HEADERS    1
     6.8  #define HAVE_ALLOCA_H       1
     6.9 -#define HAVE_SYS_TYPES_H    1
    6.10 -#define HAVE_STDIO_H    1
    6.11 -#define STDC_HEADERS    1
    6.12 -#define HAVE_STRING_H   1
    6.13 +#define HAVE_CTYPE_H    1
    6.14  #define HAVE_INTTYPES_H 1
    6.15 -#define HAVE_STDINT_H   1
    6.16 -#define HAVE_CTYPE_H    1
    6.17 +#define HAVE_LIMITS_H   1
    6.18  #define HAVE_MATH_H 1
    6.19  #define HAVE_SIGNAL_H 1
    6.20 +#define HAVE_STDINT_H   1
    6.21 +#define HAVE_STDIO_H    1
    6.22 +#define HAVE_STRING_H   1
    6.23 +#define HAVE_SYS_TYPES_H    1
    6.24  
    6.25  /* C library functions */
    6.26  #define HAVE_MALLOC 1
     7.1 --- a/include/SDL_config_iphoneos.h	Sat Sep 09 08:34:46 2017 -0700
     7.2 +++ b/include/SDL_config_iphoneos.h	Sat Sep 09 08:36:37 2017 -0700
     7.3 @@ -33,16 +33,17 @@
     7.4  
     7.5  #define HAVE_GCC_ATOMICS    1
     7.6  
     7.7 +#define STDC_HEADERS    1
     7.8  #define HAVE_ALLOCA_H       1
     7.9 -#define HAVE_SYS_TYPES_H    1
    7.10 -#define HAVE_STDIO_H    1
    7.11 -#define STDC_HEADERS    1
    7.12 -#define HAVE_STRING_H   1
    7.13 +#define HAVE_CTYPE_H    1
    7.14  #define HAVE_INTTYPES_H 1
    7.15 -#define HAVE_STDINT_H   1
    7.16 -#define HAVE_CTYPE_H    1
    7.17 +#define HAVE_LIMITS_H   1
    7.18  #define HAVE_MATH_H 1
    7.19  #define HAVE_SIGNAL_H   1
    7.20 +#define HAVE_STDINT_H   1
    7.21 +#define HAVE_STDIO_H    1
    7.22 +#define HAVE_STRING_H   1
    7.23 +#define HAVE_SYS_TYPES_H    1
    7.24  
    7.25  /* C library functions */
    7.26  #define HAVE_MALLOC 1
     8.1 --- a/include/SDL_config_macosx.h	Sat Sep 09 08:34:46 2017 -0700
     8.2 +++ b/include/SDL_config_macosx.h	Sat Sep 09 08:36:37 2017 -0700
     8.3 @@ -37,17 +37,18 @@
     8.4  #endif
     8.5  
     8.6  /* Useful headers */
     8.7 +#define STDC_HEADERS    1
     8.8  #define HAVE_ALLOCA_H       1
     8.9 +#define HAVE_CTYPE_H    1
    8.10 +#define HAVE_FLOAT_H    1
    8.11 +#define HAVE_INTTYPES_H 1
    8.12 +#define HAVE_LIMITS_H   1
    8.13 +#define HAVE_MATH_H 1
    8.14 +#define HAVE_SIGNAL_H   1
    8.15 +#define HAVE_STDINT_H   1
    8.16 +#define HAVE_STDIO_H    1
    8.17 +#define HAVE_STRING_H   1
    8.18  #define HAVE_SYS_TYPES_H    1
    8.19 -#define HAVE_STDIO_H    1
    8.20 -#define STDC_HEADERS    1
    8.21 -#define HAVE_STRING_H   1
    8.22 -#define HAVE_INTTYPES_H 1
    8.23 -#define HAVE_STDINT_H   1
    8.24 -#define HAVE_CTYPE_H    1
    8.25 -#define HAVE_MATH_H 1
    8.26 -#define HAVE_FLOAT_H    1
    8.27 -#define HAVE_SIGNAL_H   1
    8.28  
    8.29  /* C library functions */
    8.30  #define HAVE_MALLOC 1
     9.1 --- a/include/SDL_config_pandora.h	Sat Sep 09 08:34:46 2017 -0700
     9.2 +++ b/include/SDL_config_pandora.h	Sat Sep 09 08:36:37 2017 -0700
     9.3 @@ -36,22 +36,24 @@
     9.4  
     9.5  #define SDL_BYTEORDER 1234
     9.6  
     9.7 +#define STDC_HEADERS 1
     9.8  #define HAVE_ALLOCA_H 1
     9.9 +#define HAVE_CTYPE_H 1
    9.10 +#define HAVE_ICONV_H 1
    9.11 +#define HAVE_INTTYPES_H 1
    9.12 +#define HAVE_LIMITS_H 1
    9.13 +#define HAVE_MALLOC_H 1
    9.14 +#define HAVE_MATH_H 1
    9.15 +#define HAVE_MEMORY_H 1
    9.16 +#define HAVE_SIGNAL_H 1
    9.17 +#define HAVE_STDARG_H 1
    9.18 +#define HAVE_STDINT_H 1
    9.19 +#define HAVE_STDIO_H 1
    9.20 +#define HAVE_STDLIB_H 1
    9.21 +#define HAVE_STRINGS_H 1
    9.22 +#define HAVE_STRING_H 1
    9.23  #define HAVE_SYS_TYPES_H 1
    9.24 -#define HAVE_STDIO_H 1
    9.25 -#define STDC_HEADERS 1
    9.26 -#define HAVE_STDLIB_H 1
    9.27 -#define HAVE_STDARG_H 1
    9.28 -#define HAVE_MALLOC_H 1
    9.29 -#define HAVE_MEMORY_H 1
    9.30 -#define HAVE_STRING_H 1
    9.31 -#define HAVE_STRINGS_H 1
    9.32 -#define HAVE_INTTYPES_H 1
    9.33 -#define HAVE_STDINT_H 1
    9.34 -#define HAVE_CTYPE_H 1
    9.35 -#define HAVE_MATH_H 1
    9.36 -#define HAVE_ICONV_H 1
    9.37 -#define HAVE_SIGNAL_H 1
    9.38 +
    9.39  #define HAVE_MALLOC 1
    9.40  #define HAVE_CALLOC 1
    9.41  #define HAVE_REALLOC 1
    10.1 --- a/include/SDL_config_psp.h	Sat Sep 09 08:34:46 2017 -0700
    10.2 +++ b/include/SDL_config_psp.h	Sat Sep 09 08:36:37 2017 -0700
    10.3 @@ -33,16 +33,17 @@
    10.4  
    10.5  #define HAVE_GCC_ATOMICS    1
    10.6  
    10.7 +#define STDC_HEADERS    1
    10.8  #define HAVE_ALLOCA_H       1
    10.9 -#define HAVE_SYS_TYPES_H    1
   10.10 -#define HAVE_STDIO_H    1
   10.11 -#define STDC_HEADERS    1
   10.12 -#define HAVE_STRING_H   1
   10.13 +#define HAVE_CTYPE_H    1
   10.14  #define HAVE_INTTYPES_H 1
   10.15 -#define HAVE_STDINT_H   1
   10.16 -#define HAVE_CTYPE_H    1
   10.17 +#define HAVE_LIMITS_H   1
   10.18  #define HAVE_MATH_H 1
   10.19  #define HAVE_SIGNAL_H   1
   10.20 +#define HAVE_STDINT_H   1
   10.21 +#define HAVE_STDIO_H    1
   10.22 +#define HAVE_STRING_H   1
   10.23 +#define HAVE_SYS_TYPES_H    1
   10.24  
   10.25  /* C library functions */
   10.26  #define HAVE_MALLOC 1
    11.1 --- a/include/SDL_config_windows.h	Sat Sep 09 08:34:46 2017 -0700
    11.2 +++ b/include/SDL_config_windows.h	Sat Sep 09 08:36:37 2017 -0700
    11.3 @@ -86,13 +86,14 @@
    11.4  /* This is disabled by default to avoid C runtime dependencies and manifest requirements */
    11.5  #ifdef HAVE_LIBC
    11.6  /* Useful headers */
    11.7 +#define STDC_HEADERS 1
    11.8 +#define HAVE_CTYPE_H 1
    11.9 +#define HAVE_FLOAT_H 1
   11.10 +#define HAVE_LIMITS_H 1
   11.11 +#define HAVE_MATH_H 1
   11.12 +#define HAVE_SIGNAL_H 1
   11.13  #define HAVE_STDIO_H 1
   11.14 -#define STDC_HEADERS 1
   11.15  #define HAVE_STRING_H 1
   11.16 -#define HAVE_CTYPE_H 1
   11.17 -#define HAVE_MATH_H 1
   11.18 -#define HAVE_FLOAT_H 1
   11.19 -#define HAVE_SIGNAL_H 1
   11.20  
   11.21  /* C library functions */
   11.22  #define HAVE_MALLOC 1
   11.23 @@ -141,12 +142,19 @@
   11.24  #define HAVE_SQRTF 1
   11.25  #define HAVE_TAN 1
   11.26  #define HAVE_TANF 1
   11.27 +#if defined(_MSC_VER)
   11.28 +/* These functions were added with the VC++ 2013 C runtime library */
   11.29  #if _MSC_VER >= 1800
   11.30  #define HAVE_STRTOLL 1
   11.31  #define HAVE_VSSCANF 1
   11.32  #define HAVE_COPYSIGN 1
   11.33  #define HAVE_SCALBN 1
   11.34  #endif
   11.35 +/* This function is available with at least the VC++ 2008 C runtime library */
   11.36 +#if _MSC_VER >= 1400
   11.37 +#define HAVE__FSEEKI64 1
   11.38 +#endif
   11.39 +#endif
   11.40  #if !defined(_MSC_VER) || defined(_USE_MATH_DEFINES)
   11.41  #define HAVE_M_PI 1
   11.42  #endif
    12.1 --- a/include/SDL_config_winrt.h	Sat Sep 09 08:34:46 2017 -0700
    12.2 +++ b/include/SDL_config_winrt.h	Sat Sep 09 08:36:37 2017 -0700
    12.3 @@ -98,13 +98,14 @@
    12.4  #define HAVE_XINPUT_H 1
    12.5  #endif
    12.6  #define HAVE_LIBC 1
    12.7 +#define STDC_HEADERS 1
    12.8 +#define HAVE_CTYPE_H 1
    12.9 +#define HAVE_FLOAT_H 1
   12.10 +#define HAVE_LIMITS_H 1
   12.11 +#define HAVE_MATH_H 1
   12.12 +#define HAVE_SIGNAL_H 1
   12.13  #define HAVE_STDIO_H 1
   12.14 -#define STDC_HEADERS 1
   12.15  #define HAVE_STRING_H 1
   12.16 -#define HAVE_CTYPE_H 1
   12.17 -#define HAVE_MATH_H 1
   12.18 -#define HAVE_FLOAT_H 1
   12.19 -#define HAVE_SIGNAL_H 1
   12.20  
   12.21  /* C library functions */
   12.22  #define HAVE_MALLOC 1
    13.1 --- a/include/SDL_config_wiz.h	Sat Sep 09 08:34:46 2017 -0700
    13.2 +++ b/include/SDL_config_wiz.h	Sat Sep 09 08:36:37 2017 -0700
    13.3 @@ -30,22 +30,24 @@
    13.4  
    13.5  #define SDL_BYTEORDER 1234
    13.6  
    13.7 +#define STDC_HEADERS 1
    13.8  #define HAVE_ALLOCA_H 1
    13.9 +#define HAVE_CTYPE_H 1
   13.10 +#define HAVE_ICONV_H 1
   13.11 +#define HAVE_INTTYPES_H 1
   13.12 +#define HAVE_LIMITS_H 1
   13.13 +#define HAVE_MALLOC_H 1
   13.14 +#define HAVE_MATH_H 1
   13.15 +#define HAVE_MEMORY_H 1
   13.16 +#define HAVE_SIGNAL_H 1
   13.17 +#define HAVE_STDARG_H 1
   13.18 +#define HAVE_STDINT_H 1
   13.19 +#define HAVE_STDIO_H 1
   13.20 +#define HAVE_STDLIB_H 1
   13.21 +#define HAVE_STRINGS_H 1
   13.22 +#define HAVE_STRING_H 1
   13.23  #define HAVE_SYS_TYPES_H 1
   13.24 -#define HAVE_STDIO_H 1
   13.25 -#define STDC_HEADERS 1
   13.26 -#define HAVE_STDLIB_H 1
   13.27 -#define HAVE_STDARG_H 1
   13.28 -#define HAVE_MALLOC_H 1
   13.29 -#define HAVE_MEMORY_H 1
   13.30 -#define HAVE_STRING_H 1
   13.31 -#define HAVE_STRINGS_H 1
   13.32 -#define HAVE_INTTYPES_H 1
   13.33 -#define HAVE_STDINT_H 1
   13.34 -#define HAVE_CTYPE_H 1
   13.35 -#define HAVE_MATH_H 1
   13.36 -#define HAVE_ICONV_H 1
   13.37 -#define HAVE_SIGNAL_H 1
   13.38 +
   13.39  #define HAVE_MALLOC 1
   13.40  #define HAVE_CALLOC 1
   13.41  #define HAVE_REALLOC 1
    14.1 --- a/src/file/SDL_rwops.c	Sat Sep 09 08:34:46 2017 -0700
    14.2 +++ b/src/file/SDL_rwops.c	Sat Sep 09 08:36:37 2017 -0700
    14.3 @@ -32,6 +32,13 @@
    14.4  #include "../core/windows/SDL_windows.h"
    14.5  #endif
    14.6  
    14.7 +#ifdef HAVE_STDIO_H
    14.8 +#include <stdio.h>
    14.9 +#endif
   14.10 +
   14.11 +#ifdef HAVE_LIMITS_H
   14.12 +#include <limits.h>
   14.13 +#endif
   14.14  
   14.15  /* This file provides a general interface for SDL to read and write
   14.16     data sources.  It can easily be extended to files, memory, etc.
   14.17 @@ -306,6 +313,19 @@
   14.18  #define fseek   fseeko64
   14.19  #define ftell   ftello64
   14.20  #elif defined(HAVE_FSEEKO)
   14.21 +#if defined(OFF_MIN) && defined(OFF_MAX)
   14.22 +#define FSEEK_OFF_MIN OFF_MIN
   14.23 +#define FSEEK_OFF_MAX OFF_MAX
   14.24 +#elif defined(HAVE_LIMITS_H)
   14.25 +/* POSIX doesn't specify the minimum and maximum macros for off_t so
   14.26 + * we have to improvise and dance around implementation-defined
   14.27 + * behavior. This may fail if the off_t type has padding bits or
   14.28 + * is not a two's-complement representation. The compilers will detect
   14.29 + * and eliminate the dead code if off_t has 64 bits.
   14.30 + */
   14.31 +#define FSEEK_OFF_MAX (((((off_t)1 << (sizeof(off_t) * CHAR_BIT - 2)) - 1) << 1) + 1)
   14.32 +#define FSEEK_OFF_MIN (-(FSEEK_OFF_MAX) - 1)
   14.33 +#endif
   14.34  #define fseek_off_t off_t
   14.35  #define fseek   fseeko
   14.36  #define ftell   ftello
   14.37 @@ -314,6 +334,10 @@
   14.38  #define fseek   _fseeki64
   14.39  #define ftell   _ftelli64
   14.40  #else
   14.41 +#ifdef HAVE_LIMITS_H
   14.42 +#define FSEEK_OFF_MIN LONG_MIN
   14.43 +#define FSEEK_OFF_MAX LONG_MAX
   14.44 +#endif
   14.45  #define fseek_off_t long
   14.46  #endif
   14.47  
   14.48 @@ -337,8 +361,18 @@
   14.49  static Sint64 SDLCALL
   14.50  stdio_seek(SDL_RWops * context, Sint64 offset, int whence)
   14.51  {
   14.52 +#if defined(FSEEK_OFF_MIN) && defined(FSEEK_OFF_MAX)
   14.53 +    if (offset < (Sint64)(FSEEK_OFF_MIN) || offset > (Sint64)(FSEEK_OFF_MAX)) {
   14.54 +        return SDL_SetError("Seek offset out of range");
   14.55 +    }
   14.56 +#endif
   14.57 +
   14.58      if (fseek(context->hidden.stdio.fp, (fseek_off_t)offset, whence) == 0) {
   14.59 -        return ftell(context->hidden.stdio.fp);
   14.60 +        Sint64 pos = ftell(context->hidden.stdio.fp);
   14.61 +        if (pos < 0) {
   14.62 +            return SDL_SetError("Couldn't get stream offset");
   14.63 +        }
   14.64 +        return pos;
   14.65      }
   14.66      return SDL_Error(SDL_EFSEEK);
   14.67  }