Powered by Linux
Re: [PATCH] recognize binary constants — Semantic Matching Tool

Re: [PATCH] recognize binary constants

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



A small change to your patch, Dan:  I added a call to tolower(), since
"0B11" is also a valid syntax.

 -Kamal

---------------------------------------------------------------------------

From a9273391fe5f410321a0668e18beaebce086e23d Mon Sep 17 00:00:00 2001
From: Kamal Mostafa <kamal@xxxxxxxxxxxxx>
Date: Thu, 8 Sep 2011 10:34:27 -0700
Subject: [PATCH] recognize binary constants

Sparse doesn't parse binary constants properly so the following code
generates an error:

	x = 0b11;

test.c:5:17: error: constant 0b11 is not a valid number

Reported-by: Kamal Mostafa <kamal@xxxxxxxxxxxxx>
Signed-off-by: Dan Carpenter <error27@xxxxxxxxx>
Signed-off-by: Kamal Mostafa <kamal@xxxxxxxxxxxxx>
---
 expression.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/expression.c b/expression.c
index 7e06e60..07d6846 100644
--- a/expression.c
+++ b/expression.c
@@ -268,6 +268,13 @@ static struct token *string_expression(struct token *token, struct expression *e
 #define ULLONG_MAX (~0ULL)
 #endif
 
+static unsigned long long parse_num(const char *nptr, char **end)
+{
+	if (nptr[0] == '0' && tolower(nptr[1]) == 'b')
+		return strtoull(&nptr[2], end, 2);
+	return strtoull(nptr, end, 0);
+}
+
 static void get_number_value(struct expression *expr, struct token *token)
 {
 	const char *str = token->number;
@@ -279,7 +286,7 @@ static void get_number_value(struct expression *expr, struct token *token)
 	int bits;
 
 	errno = 0;
-	value = strtoull(str, &end, 0);
+	value = parse_num(str, &end);
 	if (end == str)
 		goto Float;
 	if (value == ULLONG_MAX && errno == ERANGE)
-- 
1.7.4.1


Attachment: signature.asc
Description: This is a digitally signed message part


[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux