[PATCH] gsm_utils.c: cleanup + do not use dynamic memory when decoding 7bit messages

Nico Golde nico at ngolde.de
Tue Sep 18 11:39:44 UTC 2012


---
 src/gsm/gsm_utils.c |   23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/src/gsm/gsm_utils.c b/src/gsm/gsm_utils.c
index 3f3a73f..08932d1 100644
--- a/src/gsm/gsm_utils.c
+++ b/src/gsm/gsm_utils.c
@@ -2,7 +2,7 @@
  * (C) 2008 by Daniel Willmann <daniel at totalueberwachung.de>
  * (C) 2009 by Holger Hans Peter Freyther <zecke at selfish.org>
  * (C) 2009-2010 by Harald Welte <laforge at gnumonks.org>
- * (C) 2010 by Nico Golde <nico at ngolde.de>
+ * (C) 2010-2012 by Nico Golde <nico at ngolde.de>
  *
  * All Rights Reserved
  *
@@ -65,6 +65,7 @@
 //#include <openbsc/gsm_data.h>
 #include <osmocom/core/utils.h>
 #include <osmocom/gsm/gsm_utils.h>
+#include <osmocom/core/logging.h>
 
 #include <stdlib.h>
 #include <stdint.h>
@@ -128,8 +129,8 @@ int gsm_7bit_decode_hdr(char *text, const uint8_t *user_data, uint8_t septet_l,
 	int i = 0;
 	int shift = 0;
 
-	uint8_t *rtext = calloc(septet_l, sizeof(uint8_t));
-	uint8_t tmp;
+	uint8_t data[160];
+	uint8_t ext_septet;
 
 	/* skip the user data header */
 	if (ud_hdr_ind) {
@@ -140,8 +141,13 @@ int gsm_7bit_decode_hdr(char *text, const uint8_t *user_data, uint8_t septet_l,
 		septet_l = septet_l - shift;
 	}
 
+	if(septet_l >= sizeof(data)){
+		LOGP(DLSMS, LOGL_ERROR,
+		     "Message User-Data too long\n");
+		return -EINVAL;
+	}
 	for (i = 0; i < septet_l; i++) {
-		rtext[i] =
+		data[i] =
 			((user_data[((i + shift) * 7 + 7) >> 3] <<
 			  (7 - (((i + shift) * 7 + 7) & 7))) |
 			 (user_data[((i + shift) * 7) >> 3] >>
@@ -150,20 +156,19 @@ int gsm_7bit_decode_hdr(char *text, const uint8_t *user_data, uint8_t septet_l,
 
 	for (i = 0; i < septet_l; i++) {
 		/* this is an extension character */
-		if(rtext[i] == 0x1b && i + 1 < septet_l){
-			tmp = rtext[i+1];
-			*(text++) = gsm_7bit_alphabet[0x7f + tmp];
+		if(data[i] == 0x1b && i + 1 < septet_l){
+			ext_septet = data[i+1];
+			*(text++) = gsm_7bit_alphabet[0x7f + ext_septet];
 			i++;
 			continue;
 		}
 
-		*(text++) = gsm_septet_lookup(rtext[i]);
+		*(text++) = gsm_septet_lookup(data[i]);
 	}
 
 	if (ud_hdr_ind)
 		i += shift;
 	*text = '\0';
-	free(rtext);
 
 	return i;
 }
-- 
1.7.10.4


--r5Pyd7+fXNt84Ff3--




More information about the baseband-devel mailing list