| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/gl/lib/xdectoint.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* Convert decimal strings with bounds checking and exit on error. | - | ||||||||||||
| 2 | - | |||||||||||||
| 3 | Copyright (C) 2014-2018 Free Software Foundation, Inc. | - | ||||||||||||
| 4 | - | |||||||||||||
| 5 | This program is free software: you can redistribute it and/or modify | - | ||||||||||||
| 6 | it under the terms of the GNU General Public License as published by | - | ||||||||||||
| 7 | the Free Software Foundation; either version 3 of the License, or | - | ||||||||||||
| 8 | (at your option) any later version. | - | ||||||||||||
| 9 | - | |||||||||||||
| 10 | This program is distributed in the hope that it will be useful, | - | ||||||||||||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||||||||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||||||||
| 13 | GNU General Public License for more details. | - | ||||||||||||
| 14 | - | |||||||||||||
| 15 | You should have received a copy of the GNU General Public License | - | ||||||||||||
| 16 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | - | ||||||||||||
| 17 | - | |||||||||||||
| 18 | #include <config.h> | - | ||||||||||||
| 19 | - | |||||||||||||
| 20 | #include "xdectoint.h" | - | ||||||||||||
| 21 | - | |||||||||||||
| 22 | #include <errno.h> | - | ||||||||||||
| 23 | #include <inttypes.h> | - | ||||||||||||
| 24 | #include <stdlib.h> | - | ||||||||||||
| 25 | - | |||||||||||||
| 26 | #include "error.h" | - | ||||||||||||
| 27 | #include "quote.h" | - | ||||||||||||
| 28 | #include "xstrtol.h" | - | ||||||||||||
| 29 | - | |||||||||||||
| 30 | /* Parse numeric string N_STR of base BASE, and return the value. | - | ||||||||||||
| 31 | Exit on parse error or if MIN or MAX are exceeded. | - | ||||||||||||
| 32 | Strings can have multiplicative SUFFIXES if specified. | - | ||||||||||||
| 33 | ERR is printed along with N_STR on error. */ | - | ||||||||||||
| 34 | - | |||||||||||||
| 35 | __xdectoint_t | - | ||||||||||||
| 36 | __xnumtoint (const char *n_str, int base, __xdectoint_t min, __xdectoint_t max, | - | ||||||||||||
| 37 | const char *suffixes, const char *err, int err_exit) | - | ||||||||||||
| 38 | { | - | ||||||||||||
| 39 | strtol_error s_err; | - | ||||||||||||
| 40 | - | |||||||||||||
| 41 | __xdectoint_t tnum; | - | ||||||||||||
| 42 | s_err = __xstrtol (n_str, NULL, base, &tnum, suffixes); | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | if (s_err == LONGINT_OK)
| 96-4727 | ||||||||||||
| 45 | { | - | ||||||||||||
| 46 | if (tnum < min || max < tnum)
| 5-4718 | ||||||||||||
| 47 | { | - | ||||||||||||
| 48 | s_err = LONGINT_OVERFLOW; | - | ||||||||||||
| 49 | /* Use have the INT range as a heuristic to distinguish | - | ||||||||||||
| 50 | type overflow rather than other min/max limits. */ | - | ||||||||||||
| 51 | if (tnum > INT_MAX/2)
| 2-12 | ||||||||||||
| 52 | errno = EOVERFLOW; executed 2 times by 2 tests: (*__errno_location ()) = 75 ;Executed by:
| 2 | ||||||||||||
| 53 | #if __xdectoint_signed | - | ||||||||||||
| 54 | else if (tnum < INT_MIN/2)
| 0-3 | ||||||||||||
| 55 | errno = EOVERFLOW; never executed: (*__errno_location ()) = 75 ; | 0 | ||||||||||||
| 56 | #endif | - | ||||||||||||
| 57 | else | - | ||||||||||||
| 58 | errno = ERANGE; executed 12 times by 3 tests: (*__errno_location ()) = 34 ;Executed by:
| 12 | ||||||||||||
| 59 | } | - | ||||||||||||
| 60 | } executed 4727 times by 13 tests: end of blockExecuted by:
| 4727 | ||||||||||||
| 61 | else if (s_err == LONGINT_OVERFLOW)
| 10-86 | ||||||||||||
| 62 | errno = EOVERFLOW; executed 10 times by 4 tests: (*__errno_location ()) = 75 ;Executed by:
| 10 | ||||||||||||
| 63 | else if (s_err == LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW)
| 0-86 | ||||||||||||
| 64 | errno = 0; /* Don't show ERANGE errors for invalid numbers. */ never executed: (*__errno_location ()) = 0; | 0 | ||||||||||||
| 65 | - | |||||||||||||
| 66 | if (s_err != LONGINT_OK)
| 110-4713 | ||||||||||||
| 67 | { | - | ||||||||||||
| 68 | /* EINVAL error message is redundant in this context. */ | - | ||||||||||||
| 69 | error (err_exit ? err_exit : EXIT_FAILURE, errno == EINVAL ? 0 : errno, | - | ||||||||||||
| 70 | "%s: %s", err, quote (n_str)); | - | ||||||||||||
| 71 | } never executed: end of block | 0 | ||||||||||||
| 72 | - | |||||||||||||
| 73 | return tnum; executed 4713 times by 13 tests: return tnum;Executed by:
| 4713 | ||||||||||||
| 74 | } | - | ||||||||||||
| 75 | - | |||||||||||||
| 76 | /* Parse decimal string N_STR, and return the value. | - | ||||||||||||
| 77 | Exit on parse error or if MIN or MAX are exceeded. | - | ||||||||||||
| 78 | Strings can have multiplicative SUFFIXES if specified. | - | ||||||||||||
| 79 | ERR is printed along with N_STR on error. */ | - | ||||||||||||
| 80 | - | |||||||||||||
| 81 | __xdectoint_t | - | ||||||||||||
| 82 | __xdectoint (const char *n_str, __xdectoint_t min, __xdectoint_t max, | - | ||||||||||||
| 83 | const char *suffixes, const char *err, int err_exit) | - | ||||||||||||
| 84 | { | - | ||||||||||||
| 85 | return __xnumtoint (n_str, 10, min, max, suffixes, err, err_exit); executed 4808 times by 15 tests: return xnumtoimax (n_str, 10, min, max, suffixes, err, err_exit);Executed by:
| 4808 | ||||||||||||
| 86 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |