Coverage Report

Created: 2025-08-03 06:33

/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
743k
isc_parse_uint32(uint32_t *uip, const char *string, int base) {
27
743k
  unsigned long n;
28
743k
  uint32_t r;
29
743k
  char *e;
30
743k
  if (!isalnum((unsigned char)(string[0]))) {
31
247k
    return ISC_R_BADNUMBER;
32
247k
  }
33
496k
  errno = 0;
34
496k
  n = strtoul(string, &e, base);
35
496k
  if (*e != '\0') {
36
476
    return ISC_R_BADNUMBER;
37
476
  }
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
495k
  r = (uint32_t)n;
44
495k
  if ((n == ULONG_MAX && errno == ERANGE) || (n != (unsigned long)r)) {
45
1.41k
    return ISC_R_RANGE;
46
1.41k
  }
47
494k
  *uip = r;
48
494k
  return ISC_R_SUCCESS;
49
495k
}
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
6.18k
isc_parse_uint8(uint8_t *uip, const char *string, int base) {
68
6.18k
  uint32_t val;
69
6.18k
  isc_result_t result;
70
6.18k
  result = isc_parse_uint32(&val, string, base);
71
6.18k
  if (result != ISC_R_SUCCESS) {
72
146
    return result;
73
146
  }
74
6.03k
  if (val > 0xFF) {
75
84
    return ISC_R_RANGE;
76
84
  }
77
5.95k
  *uip = (uint8_t)val;
78
5.95k
  return ISC_R_SUCCESS;
79
6.03k
}