diff --git a/src/builtin.cpp b/src/builtin.cpp index 658722bc4..2e2d35a1c 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -1588,8 +1588,8 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis e.param1.job_id = job_id; } } else { - pid_t pid; - if (!(parse_integer(w.woptarg, &pid)) || pid < 1) { + pid_t pid = wcstoimax(w.woptarg, &end, 10); + if (pid < 1 || !(*w.woptarg != L'\0' && *end == L'\0')) { append_format(*out_err, _(L"%ls: Invalid process id '%ls'"), argv[0], w.woptarg); res = STATUS_BUILTIN_ERROR; diff --git a/src/common.h b/src/common.h index 50100af8b..2bdf3e3ae 100644 --- a/src/common.h +++ b/src/common.h @@ -11,9 +11,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -674,21 +671,6 @@ ssize_t read_loop(int fd, void *buff, size_t count); void __attribute__((noinline)) debug(int level, const char *msg, ...); void __attribute__((noinline)) debug(int level, const wchar_t *msg, ...); -/// Parse an integer and check limits for type -/// The only way to be safe from overflows is intmax_t. -/// Do that, and make sure the result isn't bigger than the maximum supported for whatever type. -template -bool parse_integer(const wchar_t *in, T* out) { - wchar_t *end; - intmax_t res = wcstoimax(in, &end, 0); - - if (!(*in != L'\0' && *end == L'\0')) return false; - if (std::numeric_limits::max() < res || res < std::numeric_limits::min()) return false; - - *out = static_cast(res); - return true; -} - /// Replace special characters with backslash escape sequences. Newline is replaced with \n, etc. /// /// \param in The string to be escaped