# [PATCH] grep: remove tautological check

3 messages from 2012-10-20 to 2012-10-22. Participants: David Soria Parra, Peter Krefting.
Thread: https://gitlist.dev/t/31877

## David Soria Parra, 2012-10-20 17:26

Subject: [PATCH] grep: remove tautological check
Message-ID: <1350753964-29346-1-git-send-email-dsp@php.net>
URL: https://gitlist.dev/e/1350753964-29346-1-git-send-email-dsp%40php.net

```
The enum grep_header_field is unsigned. Therefore the field part of the
grep_pat structure is unsigned and cannot be less then 0. We remove the
tautological check for p->field < 0.

Signed-off-by: David Soria Parra <dsp@php.net>
---
 grep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/grep.c b/grep.c
index 4bd1b8b..db177ef 100644
--- a/grep.c
+++ b/grep.c
@@ -625,7 +625,7 @@ static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
 	for (p = opt->header_list; p; p = p->next) {
 		if (p->token != GREP_PATTERN_HEAD)
 			die("bug: a non-header pattern in grep header list.");
-		if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
+		if (GREP_HEADER_FIELD_MAX <= p->field)
 			die("bug: unknown header field %d", p->field);
 		compile_regexp(p, opt);
 	}
-- 
1.8.0.rc3.332.g181c802

```

## Peter Krefting, 2012-10-20 19:23

Subject: Re: [PATCH] grep: remove tautological check
Message-ID: <alpine.DEB.2.00.1210202022270.19498@ds9.cixit.se>
URL: https://gitlist.dev/e/alpine.DEB.2.00.1210202022270.19498%40ds9.cixit.se
In-Reply-To: <1350753964-29346-1-git-send-email-dsp@php.net>

```
David Soria Parra:

> The enum grep_header_field is unsigned.

Enumerations can be either unsigned or signed, it is up to the 
compiler to decide. Even if you assign only positive number to named 
enumeration values, there are compilers that make them signed. I've 
been bitten by that enough.

-- 
\\// Peter - http://www.softwolves.pp.se/

```

## Peter Krefting, 2012-10-22 13:20

Subject: Re: [PATCH] grep: remove tautological check
Message-ID: <alpine.DEB.2.00.1210221416080.32103@ds9.cixit.se>
URL: https://gitlist.dev/e/alpine.DEB.2.00.1210221416080.32103%40ds9.cixit.se
In-Reply-To: <1350753964-29346-1-git-send-email-dsp@php.net>

```
David Soria Parra:

> -		if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
> +		if (GREP_HEADER_FIELD_MAX <= p->field)

A friend taught me this trick, which will check that it isn't negative 
for compilers that have the enumeration be signed (notably MSVC), 
while not complaining for compilers that have it unsigned (GCC, Clang):

   const unsigned int sign = 1u << (sizeof(p->field) * CHAR_BIT - 1);
   if (!(sign & (unsigned int) p->field) || GREP_HEADER_FIELD_MAX <= p->field)

-- 
\\// Peter - http://www.softwolves.pp.se/

```
