[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master fba07fa 2/3: Library (arithmetic.h): types pro
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master fba07fa 2/3: Library (arithmetic.h): types properly set for integer operators |
Date: |
Fri, 19 Feb 2021 23:07:07 -0500 (EST) |
branch: master
commit fba07fa879a7e4408a4beb4e067aef43a867a4f8
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Library (arithmetic.h): types properly set for integer operators
Until now when any of the integer-only operators (like modulo, or the
bitwise operators) was given operands that weren't unsigned 8-bit integers,
the output type was mistakenly set to unsigned 8-bit integers. Therefore
creating a segmentation fault.
Having a look into the source, I recognized that the issue is due to the
function that determines the type of the output. In that function (which
hasn't been touched for a VERY LONG TIME: before the problematic operators
were introduced) I was assuming that the binary operators are plus, minus,
multiply and divide as well as the conditional operators. Since the
conditional operators only unsigned 8-bit integers, I was returning this
type for anything that is not plus, minus, multiply or divide.
With this commit, the issue was fixed by reversing the check: if
conditional operators are requested, the output will be unsigned 8-bit
integers, otherwise, it will be the larger of the two input types.
In the process, I also discovered that when reporting errors on conditional
operators, I was mistakenly using the conditional operator signs (like '<'
for 'le', or less than). This has also been fixed with this commit.
This fixes bug #60082.
---
NEWS | 1 +
lib/arithmetic.c | 28 ++++++++++++++++------------
2 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/NEWS b/NEWS
index b2ac437..4e387b3 100644
--- a/NEWS
+++ b/NEWS
@@ -51,6 +51,7 @@ See the end of the file for license conditions.
used.
** Bugs fixed
+ bug #60082: Arithmetic library crash for integer operators like modulo
diff --git a/lib/arithmetic.c b/lib/arithmetic.c
index 91acdd6..6372151 100644
--- a/lib/arithmetic.c
+++ b/lib/arithmetic.c
@@ -1512,14 +1512,18 @@ arithmetic_binary_out_type(int operator, gal_data_t *l,
gal_data_t *r)
{
switch(operator)
{
- case GAL_ARITHMETIC_OP_PLUS:
- case GAL_ARITHMETIC_OP_MINUS:
- case GAL_ARITHMETIC_OP_MULTIPLY:
- case GAL_ARITHMETIC_OP_DIVIDE:
- return gal_type_out(l->type, r->type);
+ case GAL_ARITHMETIC_OP_LT:
+ case GAL_ARITHMETIC_OP_LE:
+ case GAL_ARITHMETIC_OP_GT:
+ case GAL_ARITHMETIC_OP_GE:
+ case GAL_ARITHMETIC_OP_EQ:
+ case GAL_ARITHMETIC_OP_NE:
+ case GAL_ARITHMETIC_OP_AND:
+ case GAL_ARITHMETIC_OP_OR:
+ return GAL_TYPE_UINT8;
default:
- return GAL_TYPE_UINT8;
+ return gal_type_out(l->type, r->type);
}
return -1;
}
@@ -2033,12 +2037,12 @@ gal_arithmetic_operator_string(int operator)
case GAL_ARITHMETIC_OP_DIVIDE: return "/";
case GAL_ARITHMETIC_OP_MODULO: return "%";
- case GAL_ARITHMETIC_OP_LT: return "<";
- case GAL_ARITHMETIC_OP_LE: return "<=";
- case GAL_ARITHMETIC_OP_GT: return ">";
- case GAL_ARITHMETIC_OP_GE: return ">=";
- case GAL_ARITHMETIC_OP_EQ: return "==";
- case GAL_ARITHMETIC_OP_NE: return "!=";
+ case GAL_ARITHMETIC_OP_LT: return "lt";
+ case GAL_ARITHMETIC_OP_LE: return "le";
+ case GAL_ARITHMETIC_OP_GT: return "gt";
+ case GAL_ARITHMETIC_OP_GE: return "ge";
+ case GAL_ARITHMETIC_OP_EQ: return "eq";
+ case GAL_ARITHMETIC_OP_NE: return "ne";
case GAL_ARITHMETIC_OP_AND: return "and";
case GAL_ARITHMETIC_OP_OR: return "or";
case GAL_ARITHMETIC_OP_NOT: return "not";