Coverage Report

Created: 2025-10-10 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/compat/strtonum.c
Line
Count
Source
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
#include <errno.h>
21
#include <limits.h>
22
#include <stdlib.h>
23
24
#include "compat.h"
25
26
134
#define INVALID   1
27
156
#define TOOSMALL  2
28
419
#define TOOLARGE  3
29
30
long long
31
strtonum(const char *numstr, long long minval, long long maxval,
32
    const char **errstrp)
33
48.1k
{
34
48.1k
  long long ll = 0;
35
48.1k
  char *ep;
36
48.1k
  int error = 0;
37
48.1k
  struct errval {
38
48.1k
    const char *errstr;
39
48.1k
    int err;
40
48.1k
  } ev[4] = {
41
48.1k
    { NULL,   0 },
42
48.1k
    { "invalid",  EINVAL },
43
48.1k
    { "too small",  ERANGE },
44
48.1k
    { "too large",  ERANGE },
45
48.1k
  };
46
47
48.1k
  ev[0].err = errno;
48
48.1k
  errno = 0;
49
48.1k
  if (minval > maxval)
50
0
    error = INVALID;
51
48.1k
  else {
52
48.1k
    ll = strtoll(numstr, &ep, 10);
53
48.1k
    if (numstr == ep || *ep != '\0')
54
134
      error = INVALID;
55
47.9k
    else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
56
156
      error = TOOSMALL;
57
47.8k
    else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
58
419
      error = TOOLARGE;
59
48.1k
  }
60
48.1k
  if (errstrp != NULL)
61
48.1k
    *errstrp = ev[error].errstr;
62
48.1k
  errno = ev[error].err;
63
48.1k
  if (error)
64
709
    ll = 0;
65
66
48.1k
  return (ll);
67
48.1k
}