Skip to contentSkip to conversation

smsutil: <CR> is not necessarily a padding character in CBS message

PATCH In review Philippe Nunes 2 participants 2 messages Jul 27, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 smsutil: <CR> is not necessarily a padding character in CBS message

---
 src/smsutil.c |   37 ++++++++++++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 7 deletions(-)
src/smsutil.c +30 -7
index a541964..59eb7b1 100644
@@ -4090,7 +4090,7 @@ char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
 			unsigned char unpacked[CBS_MAX_GSM_CHARS];
 			long written;
 			int max_chars;
-			int i;
+			int i, j;
 
 			max_chars =
 				sms_text_capacity_gsm(CBS_MAX_GSM_CHARS, taken);
@@ -4102,12 +4102,24 @@ char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
 			i = iso639 ? 3 : 0;
 
 			/*
-			 * CR is a padding character, which means we can
-			 * safely discard everything afterwards
+			 * CR can be used as a padding character, which means
+			 * we can safely discard everything afterwards
 			 */
+
 			for (; i < written; i++, bufsize++) {
-				if (unpacked[i] == '\r')
-					break;
+				if (unpacked[i] == '\r') {
+					/*
+					 * check if this is a padding character or 
+					 * if it is a wanted <CR>
+					 */
+					for (j=i+1; j < written; j++)
+						if (unpacked[j] != '\r')
+							break;
+
+					if (j == written)
+						break;
+				}
+
 
 				buf[bufsize] = unpacked[i];
 			}
@@ -4135,8 +4147,19 @@ char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
 			}
 
 			while (i < max_offset) {
-				if (ud[i] == 0x00 && ud[i+1] == '\r')
-					break;
+				if (ud[i] == 0x00 && ud[i+1] == '\r') {
+					int j = i+2;
+
+					while (j < max_offset) {
+						if (ud[j] != 0x00 || ud[j+1] != '\r')
+							break;
+
+						j += 2;
+					}
+
+					if (j == max_offset)
+						break;
+				}
 
 				buf[bufsize] = ud[i];
 				buf[bufsize + 1] = ud[i+1];
Signature · 1 line
1.7.9.5
#1 lore raw

Hi Philippe,

Quoted 4 lines · Philippe Nunes
---
  src/smsutil.c |   37 ++++++++++++++++++++++++++++++-------
  1 file changed, 30 insertions(+), 7 deletions(-)

The patch looks 'correct', but a few points below:

Quoted 22 lines
src/smsutil.c +3 -3
index a541964..59eb7b1 100644
@@ -4090,7 +4090,7 @@ char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
  			unsigned char unpacked[CBS_MAX_GSM_CHARS];
  			long written;
  			int max_chars;
-			int i;
+			int i, j;
 
  			max_chars =
  				sms_text_capacity_gsm(CBS_MAX_GSM_CHARS, taken);
@@ -4102,12 +4102,24 @@ char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
  			i = iso639 ? 3 : 0;
 
  			/*
-			 * CR is a padding character, which means we can
-			 * safely discard everything afterwards
+			 * CR can be used as a padding character, which means
+			 * we can safely discard everything afterwards
  			 */

src/smsutil.c char *cbs_decode_text(GSList *cbs_list, char *iso639_lang) · line 4115

Quoted 10 lines
src/smsutil.c +7 -2
+
  			for (; i<  written; i++, bufsize++) {
-				if (unpacked[i] == '\r')
-					break;
+				if (unpacked[i] == '\r') {
+					/*
+					 * check if this is a padding character or
+					 * if it is a wanted<CR>
+					 */
+					for (j=i+1; j<  written; j++)

Why are you breaking the coding style here? Specifically item M3.

Quoted 19 lines
src/smsutil.c +10 -2
+						if (unpacked[j] != '\r')
+							break;
+
+					if (j == written)
+						break;
+				}
+
 
  				buf[bufsize] = unpacked[i];
  			}
@@ -4135,8 +4147,19 @@ char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
  			}
 
  			while (i<  max_offset) {
-				if (ud[i] == 0x00&&  ud[i+1] == '\r')
-					break;
+				if (ud[i] == 0x00&&  ud[i+1] == '\r') {
+					int j = i+2;
+

src/smsutil.c char *cbs_decode_text(GSList *cbs_list, char *iso639_lang) · line 4162

Quoted 10 lines
src/smsutil.c +10 -0
+					while (j<  max_offset) {
+						if (ud[j] != 0x00 || ud[j+1] != '\r')
+							break;
+
+						j += 2;
+					}
+
+					if (j == max_offset)
+						break;
+				}

Two points here.

First of all, you're again breaking the coding style item M3. I know the existing code is also in violation, but then the first patch in this series should be fixing that violation, and then adding the new code on top. I'm not advocating that you fix all of smsutil.c, but at least fix up the local area you will be touching first.

Secondly, mildly nitpicking, but why are you using a 'for' loop in the first chunk and now a while loop here? At the very least you can make the code shorter and be more consistent.

src/smsutil.c char *cbs_decode_text(GSList *cbs_list, char *iso639_lang) · line 4165

Quoted 3 lines
src/smsutil.c +0 -0
  				buf[bufsize] = ud[i];
  				buf[bufsize + 1] = ud[i+1];

I'd really prefer that patches like this one that fix bugs in the {sms,stk}util.c are accompanied by changes / additions to the respective unit test files. Especially in this case, as I assume that you have a set of test data that this is failing on.

That way I can immediately reference the test data, sanity check it and run the unit test to verify that the code and your changes are working as expected. Furthermore, any regressions are more easily caught in the future.

Regards,
-Denis