YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
1.13M
int32 strto32_adapter(const char *nptr, char **endptr, int base) {
28
1.13M
  const int saved_errno = errno;
29
1.13M
  errno = 0;
30
1.13M
  const auto result = strtol(nptr, endptr, base);
31
1.13M
  if (errno == ERANGE && 
result == LONG_MIN0
) {
32
0
    return kint32min;
33
1.13M
  } else if (errno == ERANGE && 
result == LONG_MAX0
) {
34
0
    return kint32max;
35
1.13M
  } else if (errno == 0 && result < kint32min) {
36
0
    errno = ERANGE;
37
0
    return kint32min;
38
1.13M
  } else if (errno == 0 && result > kint32max) {
39
0
    errno = ERANGE;
40
0
    return kint32max;
41
0
  }
42
1.13M
  if (errno == 0)
43
1.13M
    errno = saved_errno;
44
1.13M
  return static_cast<int32>(result);
45
1.13M
}
46
47
361k
uint32 strtou32_adapter(const char *nptr, char **endptr, int base) {
48
361k
  const int saved_errno = errno;
49
361k
  errno = 0;
50
361k
  const auto result = strtoul(nptr, endptr, base);
51
361k
  if (errno == ERANGE && 
result == ULONG_MAX0
) {
52
0
    return kuint32max;
53
361k
  } else 
if (errno361k
== 0361k
&& result > kuint32max) {
54
0
    errno = ERANGE;
55
0
    return kuint32max;
56
0
  }
57
361k
  if (errno == 0)
58
361k
    errno = saved_errno;
59
361k
  return static_cast<uint32>(result);
60
361k
}