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 f843eb3d31.
This commit is contained in:
ridiculousfish 2016-10-01 17:15:31 -07:00
parent 7ec205d59c
commit b05c09429d
2 changed files with 2 additions and 20 deletions

View file

@ -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;

View file

@ -11,9 +11,6 @@
#include <string.h>
#include <sys/types.h>
#include <termios.h>
#include <stddef.h>
#include <limits>
#include <inttypes.h>
#include <wchar.h>
#include <memory>
#include <sstream>
@ -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 <typename T>
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<T>::max() < res || res < std::numeric_limits<T>::min()) return false;
*out = static_cast<T>(res);
return true;
}
/// Replace special characters with backslash escape sequences. Newline is replaced with \n, etc.
///
/// \param in The string to be escaped