Fix atoi(3) out-of-range cases.

This commit is contained in:
Jonas 'Sortie' Termansen 2014-09-15 15:30:01 +02:00
parent 87fee95949
commit 94a7433cf0
1 changed files with 9 additions and 2 deletions

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011.
Copyright(C) Jonas 'Sortie' Termansen 2011, 2014.
This file is part of the Sortix C Library.
@ -22,9 +22,16 @@
*******************************************************************************/
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
extern "C" int atoi(const char* str)
{
return (int) strtol(str, (char**) NULL, 10);
long long_result = strtol(str, (char**) NULL, 10);
if ( long_result < INT_MIN )
return errno = ERANGE, INT_MIN;
if ( INT_MAX < long_result )
return errno = ERANGE, INT_MAX;
return (int) long_result;
}