Coverage Report

Created: 2023-06-07 06:23

/src/bind9/lib/isc/parseint.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*! \file */
15
16
#include <ctype.h>
17
#include <errno.h>
18
#include <inttypes.h>
19
#include <limits.h>
20
#include <stdlib.h>
21
22
#include <isc/parseint.h>
23
#include <isc/result.h>
24
25
isc_result_t
26
1.65M
isc_parse_uint32(uint32_t *uip, const char *string, int base) {
27
1.65M
  unsigned long n;
28
1.65M
  uint32_t r;
29
1.65M
  char *e;
30
1.65M
  if (!isalnum((unsigned char)(string[0]))) {
31
456k
    return (ISC_R_BADNUMBER);
32
456k
  }
33
1.20M
  errno = 0;
34
1.20M
  n = strtoul(string, &e, base);
35
1.20M
  if (*e != '\0') {
36
1.19k
    return (ISC_R_BADNUMBER);
37
1.19k
  }
38
  /*
39
   * Where long is 64 bits we need to convert to 32 bits then test for
40
   * equality.  This is a no-op on 32 bit machines and a good compiler
41
   * will optimise it away.
42
   */
43
1.20M
  r = (uint32_t)n;
44
1.20M
  if ((n == ULONG_MAX && errno == ERANGE) || (n != (unsigned long)r)) {
45
1.27k
    return (ISC_R_RANGE);
46
1.27k
  }
47
1.20M
  *uip = r;
48
1.20M
  return (ISC_R_SUCCESS);
49
1.20M
}
50
51
isc_result_t
52
0
isc_parse_uint16(uint16_t *uip, const char *string, int base) {
53
0
  uint32_t val;
54
0
  isc_result_t result;
55
0
  result = isc_parse_uint32(&val, string, base);
56
0
  if (result != ISC_R_SUCCESS) {
57
0
    return (result);
58
0
  }
59
0
  if (val > 0xFFFF) {
60
0
    return (ISC_R_RANGE);
61
0
  }
62
0
  *uip = (uint16_t)val;
63
0
  return (ISC_R_SUCCESS);
64
0
}
65
66
isc_result_t
67
76.0k
isc_parse_uint8(uint8_t *uip, const char *string, int base) {
68
76.0k
  uint32_t val;
69
76.0k
  isc_result_t result;
70
76.0k
  result = isc_parse_uint32(&val, string, base);
71
76.0k
  if (result != ISC_R_SUCCESS) {
72
126
    return (result);
73
126
  }
74
75.9k
  if (val > 0xFF) {
75
97
    return (ISC_R_RANGE);
76
97
  }
77
75.8k
  *uip = (uint8_t)val;
78
75.8k
  return (ISC_R_SUCCESS);
79
75.9k
}