From 08c97ffda24b3977477dd87e16c13bc23d1991cf Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Thu, 29 Nov 2007 00:14:14 +0100
Subject: [PATCH] Incorporated the signedness of the id_code in the
 optional_read functions as well

---
 src/lib-sieve/plugins/imapflags/tst-hasflag.c | 18 ++++++++++++++----
 src/lib-sieve/plugins/vacation/ext-vacation.c | 18 ++++++++++++++----
 src/lib-sieve/sieve-address-parts.c           | 19 +++++++++++++++----
 src/lib-sieve/sieve-code.c                    | 17 ++++++++---------
 src/lib-sieve/sieve-code.h                    |  4 ++--
 src/lib-sieve/sieve-generator.c               |  4 ++--
 src/lib-sieve/tst-header.c                    | 18 ++++++++++++++----
 7 files changed, 69 insertions(+), 29 deletions(-)

diff --git a/src/lib-sieve/plugins/imapflags/tst-hasflag.c b/src/lib-sieve/plugins/imapflags/tst-hasflag.c
index bef34f518..53a370c19 100644
--- a/src/lib-sieve/plugins/imapflags/tst-hasflag.c
+++ b/src/lib-sieve/plugins/imapflags/tst-hasflag.c
@@ -165,14 +165,19 @@ static bool tst_hasflag_opcode_dump
 (const struct sieve_opcode *opcode ATTR_UNUSED,	
 	const struct sieve_runtime_env *renv, sieve_size_t *address)
 {
-	unsigned int opt_code;
+	int opt_code = 1;
 
 	printf("HASFLAG\n");
 
 	/* Handle any optional arguments */
 	if ( sieve_operand_optional_present(renv->sbin, address) ) {
-		while ( (opt_code=sieve_operand_optional_read(renv->sbin, address)) ) {
+		while ( opt_code != 0 ) {
+			if ( !sieve_operand_optional_read(renv->sbin, address, &opt_code) ) 
+				return FALSE;
+
 			switch ( opt_code ) {
+			case 0:
+				break;
 			case OPT_COMPARATOR:
 				sieve_opr_comparator_dump(renv->sbin, address);
 				break;
@@ -197,7 +202,7 @@ static bool tst_hasflag_opcode_execute
 (const struct sieve_opcode *opcode ATTR_UNUSED,
 	const struct sieve_runtime_env *renv, sieve_size_t *address)
 {
-	unsigned int opt_code;
+	int opt_code = 1;
 	const struct sieve_comparator *cmp = &i_ascii_casemap_comparator;
 	const struct sieve_match_type *mtch = &is_match_type;
 	struct sieve_match_context *mctx;
@@ -210,8 +215,13 @@ static bool tst_hasflag_opcode_execute
 
 	/* Handle any optional arguments */
 	if ( sieve_operand_optional_present(renv->sbin, address) ) {
-		while ( (opt_code=sieve_operand_optional_read(renv->sbin, address)) ) {
+		while ( opt_code != 0 ) {
+			if ( !sieve_operand_optional_read(renv->sbin, address, &opt_code) )
+				return FALSE;
+
 			switch ( opt_code ) {
+			case 0:
+				break;
 			case OPT_COMPARATOR:
 				cmp = sieve_opr_comparator_read(renv->sbin, address);
 				break;
diff --git a/src/lib-sieve/plugins/vacation/ext-vacation.c b/src/lib-sieve/plugins/vacation/ext-vacation.c
index 3cf0b47ac..9d2cd138f 100644
--- a/src/lib-sieve/plugins/vacation/ext-vacation.c
+++ b/src/lib-sieve/plugins/vacation/ext-vacation.c
@@ -292,13 +292,18 @@ static bool ext_vacation_opcode_dump
 (const struct sieve_opcode *opcode ATTR_UNUSED,
 	const struct sieve_runtime_env *renv, sieve_size_t *address)
 {	
-	unsigned int opt_code;
+	int opt_code = 1;
 	
 	printf("VACATION\n");
 	
 	if ( sieve_operand_optional_present(renv->sbin, address) ) {
-		while ( (opt_code=sieve_operand_optional_read(renv->sbin, address)) ) {
+		while ( opt_code != 0 ) {
+			if ( !sieve_operand_optional_read(renv->sbin, address, &opt_code) ) 
+				return FALSE;
+
 			switch ( opt_code ) {
+			case 0:
+				break;
 			case OPT_DAYS:
 				if ( !sieve_opr_number_dump(renv->sbin, address) )
 					return FALSE;
@@ -333,13 +338,18 @@ static bool ext_vacation_opcode_execute
 (const struct sieve_opcode *opcode ATTR_UNUSED,
 	const struct sieve_runtime_env *renv, sieve_size_t *address)
 {	
-	unsigned int opt_code;
+	int opt_code = 1;
 	sieve_size_t days = 0;
 	string_t *reason, *subject, *from, *handle;
 		
 	if ( sieve_operand_optional_present(renv->sbin, address) ) {
-		while ( (opt_code=sieve_operand_optional_read(renv->sbin, address)) ) {
+		while ( opt_code != 0 ) {
+			if ( !sieve_operand_optional_read(renv->sbin, address, &opt_code) ) 
+				return FALSE;
+
 			switch ( opt_code ) {
+			case 0:
+				break;
 			case OPT_DAYS:
 				if ( !sieve_opr_number_read(renv->sbin, address, &days) ) return FALSE;
 				break;
diff --git a/src/lib-sieve/sieve-address-parts.c b/src/lib-sieve/sieve-address-parts.c
index 5e3479f32..4414a6521 100644
--- a/src/lib-sieve/sieve-address-parts.c
+++ b/src/lib-sieve/sieve-address-parts.c
@@ -377,11 +377,16 @@ bool sieve_address_match
 bool sieve_addrmatch_default_dump_optionals
 (struct sieve_binary *sbin, sieve_size_t *address) 
 {
-	unsigned int opt_code;
+	int opt_code = 1;
 	
 	if ( sieve_operand_optional_present(sbin, address) ) {
-		while ( (opt_code=sieve_operand_optional_read(sbin, address)) ) {
+		while ( opt_code != 0 ) {
+			if ( !sieve_operand_optional_read(sbin, address, &opt_code) ) 
+				return FALSE;
+
 			switch ( opt_code ) {
+			case 0:
+				break;
 			case SIEVE_AM_OPT_COMPARATOR:
 				if ( !sieve_opr_comparator_dump(sbin, address) )
 					return FALSE;
@@ -408,11 +413,17 @@ bool sieve_addrmatch_default_get_optionals
 	const struct sieve_address_part **addrp, const struct sieve_match_type **mtch, 
 	const struct sieve_comparator **cmp) 
 {
-	unsigned int opt_code;
+	int opt_code = 1;
+	
 	
 	if ( sieve_operand_optional_present(sbin, address) ) {
-		while ( (opt_code=sieve_operand_optional_read(sbin, address)) ) {
+		while ( opt_code != 0 ) {
+			if ( !sieve_operand_optional_read(sbin, address, &opt_code) )
+				return FALSE;
+				  
 			switch ( opt_code ) {
+			case 0:
+				break;
 			case SIEVE_AM_OPT_COMPARATOR:
 				if ( (*cmp = sieve_opr_comparator_read(sbin, address)) == NULL )
 					return FALSE;
diff --git a/src/lib-sieve/sieve-code.c b/src/lib-sieve/sieve-code.c
index 283e8ea6e..6f61f6a9f 100644
--- a/src/lib-sieve/sieve-code.c
+++ b/src/lib-sieve/sieve-code.c
@@ -137,19 +137,18 @@ bool sieve_operand_optional_present(struct sieve_binary *sbin, sieve_size_t *add
 	return FALSE;
 }
 
-unsigned int sieve_operand_optional_read(struct sieve_binary *sbin, sieve_size_t *address)
+bool sieve_operand_optional_read(struct sieve_binary *sbin, sieve_size_t *address, int *id_code)
 {
-	unsigned int id = -1;
-	
+	unsigned int id;
+
 	if ( sieve_binary_read_byte(sbin, address, &id) ) {
-		/* No more optionals */
-		if ( id == 0 ) 
-			return 0;
-			
-		return id;
+		*id_code = (int) id;
+		return TRUE;
 	}
 	
-	return -1;
+	*id_code = 0;
+
+	return FALSE;
 }
 
 /* 
diff --git a/src/lib-sieve/sieve-code.h b/src/lib-sieve/sieve-code.h
index dc18752f6..fd9f6e803 100644
--- a/src/lib-sieve/sieve-code.h
+++ b/src/lib-sieve/sieve-code.h
@@ -77,8 +77,8 @@ const struct sieve_operand *sieve_operand_read
 	(struct sieve_binary *sbin, sieve_size_t *address);
 
 bool sieve_operand_optional_present(struct sieve_binary *sbin, sieve_size_t *address);
-unsigned int sieve_operand_optional_read
-	(struct sieve_binary *sbin, sieve_size_t *address);
+bool sieve_operand_optional_read
+	(struct sieve_binary *sbin, sieve_size_t *address, int *id_code);
 
 void sieve_opr_number_emit(struct sieve_binary *sbin, sieve_size_t number);
 bool sieve_opr_number_dump(struct sieve_binary *sbin, sieve_size_t *address); 
diff --git a/src/lib-sieve/sieve-generator.c b/src/lib-sieve/sieve-generator.c
index a2230dced..c889de01b 100644
--- a/src/lib-sieve/sieve-generator.c
+++ b/src/lib-sieve/sieve-generator.c
@@ -119,7 +119,7 @@ bool sieve_generate_arguments(struct sieve_generator *generator,
 				sieve_binary_emit_byte(generator->binary, SIEVE_OPERAND_OPTIONAL);
 				
 				/* Emit argument id for optional operand */
-				sieve_binary_emit_byte(generator->binary, arg->arg_id_code);
+				sieve_binary_emit_byte(generator->binary, (unsigned char) arg->arg_id_code);
 
 				state = ARG_OPTIONAL;
 			}
@@ -129,7 +129,7 @@ bool sieve_generate_arguments(struct sieve_generator *generator,
 				state = ARG_POSITIONAL;
 			
 			/* Emit argument id for optional operand (0 marks the end of the optionals) */
-			sieve_binary_emit_byte(generator->binary, arg->arg_id_code);
+			sieve_binary_emit_byte(generator->binary, (unsigned char) arg->arg_id_code);
 
 			break;
 		case ARG_POSITIONAL:
diff --git a/src/lib-sieve/tst-header.c b/src/lib-sieve/tst-header.c
index 02ade4a97..48125b8ab 100644
--- a/src/lib-sieve/tst-header.c
+++ b/src/lib-sieve/tst-header.c
@@ -117,14 +117,19 @@ static bool tst_header_opcode_dump
 (const struct sieve_opcode *opcode ATTR_UNUSED,
 	const struct sieve_runtime_env *renv, sieve_size_t *address)
 {
-	unsigned int opt_code;
+	int opt_code = 1;
 
 	printf("HEADER\n");
 
 	/* Handle any optional arguments */
 	if ( sieve_operand_optional_present(renv->sbin, address) ) {
-		while ( (opt_code=sieve_operand_optional_read(renv->sbin, address)) ) {
+		while ( opt_code != 0 ) {
+			if ( !sieve_operand_optional_read(renv->sbin, address, &opt_code) ) 
+				return FALSE;
+
 			switch ( opt_code ) {
+			case 0:
+				break;
 			case OPT_COMPARATOR:
 				sieve_opr_comparator_dump(renv->sbin, address);
 				break;
@@ -148,7 +153,7 @@ static bool tst_header_opcode_execute
 (const struct sieve_opcode *opcode ATTR_UNUSED, 
 	const struct sieve_runtime_env *renv, sieve_size_t *address)
 {
-	unsigned int opt_code;
+	int opt_code = 1;
 	const struct sieve_comparator *cmp = &i_octet_comparator;
 	const struct sieve_match_type *mtch = &is_match_type;
 	struct sieve_match_context *mctx;
@@ -161,8 +166,13 @@ static bool tst_header_opcode_execute
 
 	/* Handle any optional arguments */
 	if ( sieve_operand_optional_present(renv->sbin, address) ) {
-		while ( (opt_code=sieve_operand_optional_read(renv->sbin, address)) ) {
+		while ( opt_code != 0 ) {
+			if ( !sieve_operand_optional_read(renv->sbin, address, &opt_code) )
+				return FALSE;
+
 			switch ( opt_code ) {
+			case 0: 
+				break;
 			case OPT_COMPARATOR:
 				cmp = sieve_opr_comparator_read(renv->sbin, address);
 				break;
-- 
GitLab