Coverage Report

Created: 2024-11-21 21:13

/root/doris/be/src/gutil/strtoint.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2008 Google Inc. All Rights Reserved.
2
//
3
// Architecture-neutral plug compatible replacements for strtol() friends.
4
// See strtoint.h for details on how to use this component.
5
//
6
7
#include "gutil/strtoint.h"
8
9
#include <errno.h>
10
#include <limits.h>
11
#include <limits>
12
13
// Replacement strto[u]l functions that have identical overflow and underflow
14
// characteristics for both ILP-32 and LP-64 platforms, including errno
15
// preservation for error-free calls.
16
0
int32 strto32_adapter(const char* nptr, char** endptr, int base) {
17
0
    const int saved_errno = errno;
18
0
    errno = 0;
19
0
    const long result = strtol(nptr, endptr, base);
20
0
    if (errno == ERANGE && result == LONG_MIN) {
21
0
        return std::numeric_limits<int>::min();
22
0
    } else if (errno == ERANGE && result == LONG_MAX) {
23
0
        return std::numeric_limits<int>::max();
24
0
    } else if (errno == 0 && result < std::numeric_limits<int>::min()) {
25
0
        errno = ERANGE;
26
0
        return std::numeric_limits<int>::min();
27
0
    } else if (errno == 0 && result > std::numeric_limits<int>::max()) {
28
0
        errno = ERANGE;
29
0
        return std::numeric_limits<int>::max();
30
0
    }
31
0
    if (errno == 0) errno = saved_errno;
32
0
    return static_cast<int32>(result);
33
0
}
34
35
0
uint32 strtou32_adapter(const char* nptr, char** endptr, int base) {
36
0
    const int saved_errno = errno;
37
0
    errno = 0;
38
0
    const unsigned long result = strtoul(nptr, endptr, base);
39
0
    if (errno == ERANGE && result == ULONG_MAX) {
40
0
        return std::numeric_limits<unsigned int>::max();
41
0
    } else if (errno == 0 && result > std::numeric_limits<unsigned int>::max()) {
42
0
        errno = ERANGE;
43
0
        return std::numeric_limits<unsigned int>::max();
44
0
    }
45
0
    if (errno == 0) errno = saved_errno;
46
0
    return static_cast<uint32>(result);
47
0
}