summaryrefslogtreecommitdiff
path: root/src/location.c
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2019-03-12 19:09:10 +0100
committerAkim Demaille <akim.demaille@gmail.com>2019-03-17 13:21:25 +0100
commit35add841ee3d5e499af0f9cfae227dc3ce62227d (patch)
tree3ce4c18bd378a70ee5e167bd2f7224d506f3f64d /src/location.c
parentf6e38d7ac97aaeb77a97e3dc0af047c89a7e4402 (diff)
downloadbison-35add841ee3d5e499af0f9cfae227dc3ce62227d.tar.gz
address warnings from GCC's UB sanitizer
Running with CC='gcc-mp-8 -fsanitize=undefined' revealed Undefined Behaviors. https://lists.gnu.org/archive/html/bison-patches/2019-03/msg00008.html * src/state.c (errs_new): Don't call memcpy with NULL as source. * src/location.c (add_column_width): Don't assume that the column argument is nonnegative: the scanner sometimes "backtracks" (e.g., see ROLLBACK_CURRENT_TOKEN and DEPRECATED) in which case we can have negative column numbers (temporarily). Found in test 3 (Invalid inputs).
Diffstat (limited to 'src/location.c')
-rw-r--r--src/location.c23
1 files changed, 7 insertions, 16 deletions
diff --git a/src/location.c b/src/location.c
index 3eae89be..38c84693 100644
--- a/src/location.c
+++ b/src/location.c
@@ -31,25 +31,16 @@ location const empty_location = EMPTY_LOCATION_INIT;
/* If BUF is null, add BUFSIZE (which in this case must be less than
INT_MAX) to COLUMN; otherwise, add mbsnwidth (BUF, BUFSIZE, 0) to
- COLUMN. If an overflow occurs, or might occur but is undetectable,
- return INT_MAX. Assume COLUMN is nonnegative. */
+ COLUMN. If an overflow occurs, return INT_MAX. */
static inline int
add_column_width (int column, char const *buf, size_t bufsize)
{
- size_t width;
- unsigned remaining_columns = INT_MAX - column;
-
- if (buf)
- {
- if (INT_MAX / 2 <= bufsize)
- return INT_MAX;
- width = mbsnwidth (buf, bufsize, 0);
- }
- else
- width = bufsize;
-
- return width <= remaining_columns ? column + width : INT_MAX;
+ int width
+ = buf ? mbsnwidth (buf, bufsize, 0)
+ : INT_MAX <= bufsize ? INT_MAX
+ : bufsize;
+ return column <= INT_MAX - width ? column + width : INT_MAX;
}
/* Set *LOC and adjust scanner cursor to account for token TOKEN of
@@ -66,7 +57,7 @@ location_compute (location *loc, boundary *cur, char const *token, size_t size)
loc->start = *cur;
- for (p = token; p < lim; p++)
+ for (p = token; p < lim; ++p)
switch (*p)
{
case '\n':