From b05c09429d54ecc0bfb05b8e0f2f7f1f67a40490 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 1 Oct 2016 17:15:31 -0700 Subject: [PATCH] Revert "Add a template to parse integers easily/correctly (#3405)" The template has different behavior around interpreting non-decimal sequences. This doesn't seem to have been intended. This reverts commit f843eb3d31bf9f0a77c4ceaf07f40e792a13b17b. --- src/builtin.cpp | 4 ++-- src/common.h | 18 ------------------ 2 files changed, 2 insertions(+), 20 deletions(-) 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