Coverage Report

Created: 2026-08-31 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lldpd/src/compat/strtonum.c
Line
Count
Source
1
/* -*- mode: c; c-file-style: "openbsd" -*- */
2
3
/*  $OpenBSD: strtonum.c,v 1.8 2015/09/13 08:31:48 guenther Exp $ */
4
5
/*
6
 * Copyright (c) 2004 Ted Unangst and Todd Miller
7
 * All rights reserved.
8
 *
9
 * Permission to use, copy, modify, and distribute this software for any
10
 * purpose with or without fee is hereby granted, provided that the above
11
 * copyright notice and this permission notice appear in all copies.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
 */
21
22
#include <errno.h>
23
#include <limits.h>
24
#include <stdlib.h>
25
#include "compat.h"
26
27
0
#define INVALID 1
28
0
#define TOOSMALL 2
29
0
#define TOOLARGE 3
30
31
long long
32
strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp)
33
0
{
34
0
  long long ll = 0;
35
0
  int error = 0;
36
0
  char *ep;
37
0
  struct errval {
38
0
    const char *errstr;
39
0
    int err;
40
0
  } ev[4] = {
41
0
    { NULL, 0 },
42
0
    { "invalid", EINVAL },
43
0
    { "too small", ERANGE },
44
0
    { "too large", ERANGE },
45
0
  };
46
47
0
  ev[0].err = errno;
48
0
  errno = 0;
49
0
  if (minval > maxval) {
50
0
    error = INVALID;
51
0
  } else {
52
0
    ll = strtoll(numstr, &ep, 10);
53
0
    if (numstr == ep || *ep != '\0')
54
0
      error = INVALID;
55
0
    else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
56
0
      error = TOOSMALL;
57
0
    else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
58
0
      error = TOOLARGE;
59
0
  }
60
0
  if (errstrp != NULL) *errstrp = ev[error].errstr;
61
0
  errno = ev[error].err;
62
0
  if (error) ll = 0;
63
64
0
  return (ll);
65
0
}