Coverage Report

Created: 2024-06-18 06:23

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