[Asterisk-code-review] Replace calls to strtok() with strtok_r() (...asterisk[13])

Sean Bright asteriskteam at digium.com
Wed Mar 6 15:08:14 CST 2019


Sean Bright has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/11099


Change subject: Replace calls to strtok() with strtok_r()
......................................................................

Replace calls to strtok() with strtok_r()

strtok() uses a static buffer, making it not thread safe.

Change-Id: Icce265153e1e65adafa8849334438ab6d190e541
---
M channels/chan_sip.c
M pbx/pbx_dundi.c
M res/res_fax.c
3 files changed, 18 insertions(+), 13 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/99/11099/1

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index e1f6f13..ac922fa 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -11583,15 +11583,16 @@
 				ast_verbose("Discarded description format %s for ID %u\n", mimeSubtype, codec);
 		}
 	} else if (!strncmp(a, red_fmtp, strlen(red_fmtp))) {
+		char *rest;
 		/* count numbers of generations in fmtp */
 		red_cp = &red_fmtp[strlen(red_fmtp)];
 		strncpy(red_fmtp, a, 100);
 
 		sscanf(red_cp, "%30u", (unsigned *)&red_data_pt[*red_num_gen]);
-		red_cp = strtok(red_cp, "/");
+		red_cp = strtok_r(red_cp, "/", &rest);
 		while (red_cp && (*red_num_gen)++ < AST_RED_MAX_GENERATION) {
 			sscanf(red_cp, "%30u", (unsigned *)&red_data_pt[*red_num_gen]);
-			red_cp = strtok(NULL, "/");
+			red_cp = strtok_r(NULL, "/", &rest);
 		}
 		red_cp = red_fmtp;
 		found = TRUE;
diff --git a/pbx/pbx_dundi.c b/pbx/pbx_dundi.c
index d2c9877..5c36c5a 100644
--- a/pbx/pbx_dundi.c
+++ b/pbx/pbx_dundi.c
@@ -2955,6 +2955,8 @@
 	db_tree = ast_db_gettree("dundi/cache", NULL);
 	ast_cli(a->fd, FORMAT2, "Number", "Context", "Expiration", "From", "Weight", "Destination (Flags)");
 	for (db_entry = db_tree; db_entry; db_entry = db_entry->next) {
+		char *rest;
+
 		if ((strncmp(db_entry->key, "/dundi/cache/hint/", 18) == 0) || ast_get_time_t(db_entry->data, &ts, 0, &length)) {
 			continue;
 		}
@@ -2966,10 +2968,10 @@
 		}
 
 		ptr = db_entry->key + sizeof("/dundi/cache");
-		strtok(ptr, "/");
-		number = strtok(NULL, "/");
-		context = strtok(NULL, "/");
-		ptr = strtok(NULL, "/");
+		strtok_r(ptr, "/", &rest);
+		number = strtok_r(NULL, "/", &rest);
+		context = strtok_r(NULL, "/", &rest);
+		ptr = strtok_r(NULL, "/", &rest);
 
 		if (*ptr != 'e') {
 			continue;
@@ -3047,6 +3049,8 @@
 	ast_cli(a->fd, FORMAT2, "Prefix", "Context", "Expiration", "From");
 
 	for (db_entry = db_tree; db_entry; db_entry = db_entry->next) {
+		char *rest;
+
 		if (ast_get_time_t(db_entry->data, &ts, 0, &length)) {
 			continue;
 		}
@@ -3058,10 +3062,10 @@
 		}
 
 		ptr = db_entry->key + sizeof("/dundi/cache/hint");
-		src = strtok(ptr, "/");
-		number = strtok(NULL, "/");
-		context = strtok(NULL, "/");
-		ptr = strtok(NULL, "/");
+		src = strtok_r(ptr, "/", &rest);
+		number = strtok_r(NULL, "/", &rest);
+		context = strtok_r(NULL, "/", &rest);
+		ptr = strtok_r(NULL, "/", &rest);
 
 		if (*ptr != 'e') {
 			continue;
diff --git a/res/res_fax.c b/res/res_fax.c
index b7aadc1..37a5561 100644
--- a/res/res_fax.c
+++ b/res/res_fax.c
@@ -809,17 +809,17 @@
 
 static int update_modem_bits(enum ast_fax_modems *bits, const char *value)
 {
-	char *m[5], *tok, *v = (char *)value;
+	char *m[5], *tok, *v = (char *) value, *rest;
 	int i = 0, j;
 
 	if (!strchr(v, ',')) {
 		m[i++] = v;
 		m[i] = NULL;
 	} else {
-		tok = strtok(v, ", ");
+		tok = strtok_r(v, ", ", &rest);
 		while (tok && i < ARRAY_LEN(m) - 1) {
 			m[i++] = tok;
-			tok = strtok(NULL, ", ");
+			tok = strtok_r(NULL, ", ", &rest);
 		}
 		m[i] = NULL;
 	}

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/11099
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Change-Id: Icce265153e1e65adafa8849334438ab6d190e541
Gerrit-Change-Number: 11099
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190306/8c218446/attachment-0001.html>


More information about the asterisk-code-review mailing list