diff options
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/common/strtol.cc | 69 | ||||
-rw-r--r-- | src/common/strtol.h | 24 | ||||
-rw-r--r-- | src/mon/MDSMonitor.cc | 1 | ||||
-rw-r--r-- | src/mon/Monitor.cc | 51 | ||||
-rw-r--r-- | src/mon/Monitor.h | 2 | ||||
-rw-r--r-- | src/mon/OSDMonitor.cc | 1 |
7 files changed, 98 insertions, 52 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 6e09dd5579b..73e4e332081 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -599,6 +599,7 @@ libcommon_files = \ include/ceph_frag.cc \ common/config.cc \ common/utf8.c \ + common/strtol.cc \ common/page.cc \ common/lockdep.cc \ common/DoutStreambuf.cc \ @@ -767,6 +768,7 @@ noinst_HEADERS = \ common/config.h\ common/ceph_crypto.h\ common/utf8.h\ + common/strtol.h\ crush/CrushWrapper.h\ crush/CrushWrapper.i\ crush/builder.h\ diff --git a/src/common/strtol.cc b/src/common/strtol.cc new file mode 100644 index 00000000000..a94c5f7b97a --- /dev/null +++ b/src/common/strtol.cc @@ -0,0 +1,69 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include <errno.h> +#include <limits.h> +#include <sstream> +#include <stdlib.h> +#include <string> + +using std::ostringstream; + +long long strict_strtoll(const char *str, int base, std::string *err) +{ + char *endptr; + errno = 0; /* To distinguish success/failure after call (see man page) */ + long long ret = strtoll(str, &endptr, base); + + if ((errno == ERANGE && (ret == LLONG_MAX || ret == LLONG_MIN)) + || (errno != 0 && ret == 0)) { + ostringstream oss; + oss << "strict_strtoll: integer underflow or overflow parsing '" << str << "'"; + *err = oss.str(); + return 0; + } + if (endptr == str) { + ostringstream oss; + oss << "strict_strtoll: expected integer, got: '" << str << "'"; + *err = oss.str(); + return 0; + } + if (*endptr != '\0') { + ostringstream oss; + oss << "strict_strtoll: garbage at end of string. got: '" << str << "'"; + *err = oss.str(); + return 0; + } + return ret; +} + +int strict_strtol(const char *str, int base, std::string *err) +{ + long long ret = strict_strtoll(str, base, err); + if (!err->empty()) + return 0; + if (ret <= INT_MIN) { + ostringstream oss; + oss << "strict_strtol: integer underflow parsing '" << str << "'"; + *err = oss.str(); + return 0; + } + if (ret >= INT_MAX) { + ostringstream oss; + oss << "strict_strtol: integer overflow parsing '" << str << "'"; + *err = oss.str(); + return 0; + } + return static_cast<int>(ret); +} diff --git a/src/common/strtol.h b/src/common/strtol.h new file mode 100644 index 00000000000..31cac53dfab --- /dev/null +++ b/src/common/strtol.h @@ -0,0 +1,24 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef CEPH_COMMON_STRTOL_H +#define CEPH_COMMON_STRTOL_H + +#include <string> + +long long strict_strtoll(const char *str, int base, std::string *err); + +int strict_strtol(const char *str, int base, std::string *err); + +#endif diff --git a/src/mon/MDSMonitor.cc b/src/mon/MDSMonitor.cc index 8402a5ff6a9..3223d6dc61a 100644 --- a/src/mon/MDSMonitor.cc +++ b/src/mon/MDSMonitor.cc @@ -18,6 +18,7 @@ #include "MonitorStore.h" #include "OSDMonitor.h" +#include "common/strtol.h" #include "messages/MMDSMap.h" #include "messages/MMDSBeacon.h" #include "messages/MMDSLoadTargets.h" diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index a426ba1d665..e9bf6745849 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -39,6 +39,7 @@ #include "messages/MAuthReply.h" +#include "common/strtol.h" #include "common/ceph_argparse.h" #include "common/Timer.h" #include "common/Clock.h" @@ -58,8 +59,6 @@ #include "common/config.h" -#include <errno.h> -#include <limits.h> #include <sstream> #include <stdlib.h> @@ -1137,51 +1136,3 @@ bool Monitor::ms_verify_authorizer(Connection *con, int peer_type, } return true; }; - -static long long strict_strtoll(const char *str, int base, std::string *err) -{ - char *endptr; - errno = 0; /* To distinguish success/failure after call (see man page) */ - long long ret = strtoll(str, &endptr, base); - - if ((errno == ERANGE && (ret == LLONG_MAX || ret == LLONG_MIN)) - || (errno != 0 && ret == 0)) { - ostringstream oss; - oss << "strict_strtoll: integer underflow or overflow parsing '" << str << "'"; - *err = oss.str(); - return 0; - } - if (endptr == str) { - ostringstream oss; - oss << "strict_strtoll: expected integer, got: '" << str << "'"; - *err = oss.str(); - return 0; - } - if (*endptr != '\0') { - ostringstream oss; - oss << "strict_strtoll: garbage at end of string. got: '" << str << "'"; - *err = oss.str(); - return 0; - } - return ret; -} - -int strict_strtol(const char *str, int base, std::string *err) -{ - long long ret = strict_strtoll(str, base, err); - if (!err->empty()) - return 0; - if (ret <= INT_MIN) { - ostringstream oss; - oss << "strict_strtol: integer underflow parsing '" << str << "'"; - *err = oss.str(); - return 0; - } - if (ret >= INT_MAX) { - ostringstream oss; - oss << "strict_strtol: integer overflow parsing '" << str << "'"; - *err = oss.str(); - return 0; - } - return static_cast<int>(ret); -} diff --git a/src/mon/Monitor.h b/src/mon/Monitor.h index 798b758c87a..2ea7da1e446 100644 --- a/src/mon/Monitor.h +++ b/src/mon/Monitor.h @@ -232,8 +232,6 @@ private: Monitor& operator=(const Monitor &rhs); }; -int strict_strtol(const char *str, int base, std::string *err); - #define CEPH_MON_FEATURE_INCOMPAT_BASE CompatSet::Feature (1, "initial feature set (~v.18)") extern const CompatSet::Feature ceph_mon_feature_compat[]; extern const CompatSet::Feature ceph_mon_feature_ro_compat[]; diff --git a/src/mon/OSDMonitor.cc b/src/mon/OSDMonitor.cc index 8848390fc0f..4a20661121c 100644 --- a/src/mon/OSDMonitor.cc +++ b/src/mon/OSDMonitor.cc @@ -21,6 +21,7 @@ #include "crush/CrushWrapper.h" +#include "common/strtol.h" #include "messages/MOSDFailure.h" #include "messages/MOSDMap.h" #include "messages/MOSDBoot.h" |