/Users/deen/code/yugabyte-db/src/yb/gutil/strtoint.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2008 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // The following only applies to changes made to this file as part of YugaByte development. |
4 | | // |
5 | | // Portions Copyright (c) YugaByte, Inc. |
6 | | // |
7 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
8 | | // in compliance with the License. You may obtain a copy of the License at |
9 | | // |
10 | | // http://www.apache.org/licenses/LICENSE-2.0 |
11 | | // |
12 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
13 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
14 | | // or implied. See the License for the specific language governing permissions and limitations |
15 | | // under the License. |
16 | | // |
17 | | // Architecture-neutral plug compatible replacements for strtol() friends. |
18 | | // See strtoint.h for details on how to use this component. |
19 | | // |
20 | | |
21 | | #include "yb/gutil/port.h" |
22 | | #include "yb/gutil/strtoint.h" |
23 | | |
24 | | // Replacement strto[u]l functions that have identical overflow and underflow |
25 | | // characteristics for both ILP-32 and LP-64 platforms, including errno |
26 | | // preservation for error-free calls. |
27 | 559k | int32 strto32_adapter(const char *nptr, char **endptr, int base) { |
28 | 559k | const int saved_errno = errno; |
29 | 559k | errno = 0; |
30 | 559k | const auto result = strtol(nptr, endptr, base); |
31 | 559k | if (errno == ERANGE && result == LONG_MIN) { |
32 | 0 | return kint32min; |
33 | 559k | } else if (errno == ERANGE && result == LONG_MAX) { |
34 | 0 | return kint32max; |
35 | 559k | } else if (errno == 0 && result < kint32min) { |
36 | 0 | errno = ERANGE; |
37 | 0 | return kint32min; |
38 | 559k | } else if (errno == 0 && result > kint32max) { |
39 | 0 | errno = ERANGE; |
40 | 0 | return kint32max; |
41 | 0 | } |
42 | 559k | if (errno == 0) |
43 | 559k | errno = saved_errno; |
44 | 559k | return static_cast<int32>(result); |
45 | 559k | } |
46 | | |
47 | 235k | uint32 strtou32_adapter(const char *nptr, char **endptr, int base) { |
48 | 235k | const int saved_errno = errno; |
49 | 235k | errno = 0; |
50 | 235k | const auto result = strtoul(nptr, endptr, base); |
51 | 235k | if (errno == ERANGE && result == ULONG_MAX) { |
52 | 0 | return kuint32max; |
53 | 235k | } else if (errno == 0 && result > kuint32max) { |
54 | 0 | errno = ERANGE; |
55 | 0 | return kuint32max; |
56 | 0 | } |
57 | 235k | if (errno == 0) |
58 | 235k | errno = saved_errno; |
59 | 235k | return static_cast<uint32>(result); |
60 | 235k | } |