From 94a7433cf098b9f758a4b75757be9da9b81db803 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 15 Sep 2014 15:30:01 +0200 Subject: [PATCH] Fix atoi(3) out-of-range cases. --- libc/stdlib/atoi.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libc/stdlib/atoi.cpp b/libc/stdlib/atoi.cpp index 36ef6c57..46454ee7 100644 --- a/libc/stdlib/atoi.cpp +++ b/libc/stdlib/atoi.cpp @@ -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 +#include #include 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; }