Skip to contentSkip to conversation

strtok -> strsep (The Easy Cases)

PATCH In review Rene Scharfe 2 participants 4 messages May 1, 2001 – May 4, 2001

Subsystems (12)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 strtok -> strsep (The Easy Cases)

Hello,

the patch at the bottom does the bulk job of strtok replacement. It's a very boring patch, containing easy cases, only. It became a bit big, too, but I trust you can digest it nevertheless. It's made against kernel version 2.4.4.

What is the benefit of getting rid of strtok? It is for cutting strings into pieces and it's used in argument parsing code of most file systems and framebuffers. The problem is: strtok is not reentrant and its manpage tells you to stop using it. There is a replacement function:
strsep. strsep has the added benefit of returning empty tokens, too. We don't need strtok, it's a bug - do you need any more reasons for replacing it? In the longer run I want the kernel to be completely cleaned from strtok - expect more patches to come.

Please apply this patch to the official version of the kernel.

René

arch/m68k/atari/config.c +4 -2
diff -ur linux-2.4.4/arch/m68k/atari/config.c linux-2.4.4-rs/arch/m68k/atari/config.c
@@ -206,13 +206,15 @@
     char *p;
     int ovsc_shift;
 
-    /* copy string to local array, strtok works destructively... */
+    /* copy string to local array, strsep works destructively... */
     strncpy( switches, str, len );
     switches[len] = 0;
     atari_switches = 0;
 
     /* parse the options */
-    for( p = strtok( switches, "," ); p; p = strtok( NULL, "," ) ) {
+    while( p = strsep( &switches, "," ) ) {
+	if (!*p)
+		continue;
 	ovsc_shift = 0;
 	if (strncmp( p, "ov_", 3 ) == 0) {
 	    p += 3;
drivers/scsi/ibmmca.c +2 -3
diff -ur linux-2.4.4/drivers/scsi/ibmmca.c linux-2.4.4-rs/drivers/scsi/ibmmca.c
@@ -1406,9 +1406,9 @@
    io_base = 0;
    id_base = 0;
    if (str) {
-      token = strtok(str,",");
       j = 0;
-      while (token) {
+      while (token = strsep(&str,",")) {
+	 if (!*token) continue;
 	 if (!strcmp(token,"activity")) display_mode |= LED_ACTIVITY;
 	 if (!strcmp(token,"display")) display_mode |= LED_DISP;
 	 if (!strcmp(token,"adisplay")) display_mode |= LED_ADISP;
@@ -1424,7 +1424,6 @@
 	      scsi_id[id_base++] = simple_strtoul(token,NULL,0);
 	    j++;
 	 }
-	 token = strtok(NULL,",");
       }
    } else if (ints) {
       for (i = 0; i < IM_MAX_HOSTS && 2*i+2 < ints[0]; i++) {
drivers/video/acornfb.c +1 -1
diff -ur linux-2.4.4/drivers/video/acornfb.c linux-2.4.4-rs/drivers/video/acornfb.c
@@ -1527,7 +1527,7 @@
 
 	acornfb_init_fbinfo();
 
-	for (opt = strtok(options, ","); opt; opt = strtok(NULL, ",")) {
+	while (opt = strsep(&options, ",")) {
 		if (!*opt)
 			continue;
 
drivers/video/amifb.c +3 -1
diff -ur linux-2.4.4/drivers/video/amifb.c linux-2.4.4-rs/drivers/video/amifb.c
@@ -1195,7 +1195,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt; this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strcmp(this_opt, "inverse")) {
 			amifb_inverse = 1;
 			fb_invert_cmaps();
drivers/video/atafb.c +1 -1
diff -ur linux-2.4.4/drivers/video/atafb.c linux-2.4.4-rs/drivers/video/atafb.c
@@ -2899,7 +2899,7 @@
     if (!options || !*options)
 		return 0;
      
-    for(this_opt=strtok(options,","); this_opt; this_opt=strtok(NULL,",")) {
+    while (this_opt = strsep(&options, ",")) {
 	if (!*this_opt) continue;
 	if ((temp=get_video_mode(this_opt)))
 		default_par=temp;
drivers/video/aty128fb.c +3 -2
diff -ur linux-2.4.4/drivers/video/aty128fb.c linux-2.4.4-rs/drivers/video/aty128fb.c
@@ -1614,8 +1614,9 @@
     if (!options || !*options)
 	return 0;
 
-    for (this_opt = strtok(options, ","); this_opt;
-	 this_opt = strtok(NULL, ",")) {
+    while (this_opt = strsep(&options, ",")) {
+	if (!*this_opt)
+		continue;
 	if (!strncmp(this_opt, "font:", 5)) {
 	    char *p;
 	    int i;
drivers/video/atyfb.c +3 -2
diff -ur linux-2.4.4/drivers/video/atyfb.c linux-2.4.4-rs/drivers/video/atyfb.c
@@ -4062,8 +4062,9 @@
     if (!options || !*options)
 	return 0;
 
-    for (this_opt = strtok(options, ","); this_opt;
-	 this_opt = strtok(NULL, ",")) {
+    while (this_opt = strsep(&options, ",")) {
+	if (!*this_opt)
+		continue;
 	if (!strncmp(this_opt, "font:", 5)) {
 		char *p;
 		int i;
drivers/video/clgenfb.c +1 -2
diff -ur linux-2.4.4/drivers/video/clgenfb.c linux-2.4.4-rs/drivers/video/clgenfb.c
@@ -2814,8 +2814,7 @@
 	if (!options || !*options)
 		return 0;
 	
-	for (this_opt = strtok (options, ","); this_opt != NULL;
-	     this_opt = strtok (NULL, ",")) {
+	while (this_opt = strsep (&options, ",")) {
 		if (!*this_opt) continue;
 		
 		DPRINTK("clgenfb_setup: option '%s'\n", this_opt);
drivers/video/controlfb.c +3 -2
diff -ur linux-2.4.4/drivers/video/controlfb.c linux-2.4.4-rs/drivers/video/controlfb.c
@@ -1211,8 +1211,9 @@
 	if (!options || !*options)
 		return;
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strncmp(this_opt, "font:", 5)) {
 			char *p;
 			int i;
drivers/video/cyberfb.c +3 -2
diff -ur linux-2.4.4/drivers/video/cyberfb.c linux-2.4.4-rs/drivers/video/cyberfb.c
@@ -1022,8 +1022,9 @@
 		return 0;
 	}
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strcmp(this_opt, "inverse")) {
 			Cyberfb_inverse = 1;
 			fb_invert_cmaps();
drivers/video/fm2fb.c +3 -2
diff -ur linux-2.4.4/drivers/video/fm2fb.c linux-2.4.4-rs/drivers/video/fm2fb.c
@@ -430,8 +430,9 @@
     if (!options || !*options)
 	return 0;
 
-    for (this_opt = strtok(options, ","); this_opt;
-	 this_opt = strtok(NULL, ",")) {
+    while (this_opt = strsep(&options, ",")) {
+	if (!*this_opt)
+		continue;
 	if (!strncmp(this_opt, "pal", 3))
 	    fm2fb_mode = FM2FB_MODE_PAL;
 	else if (!strncmp(this_opt, "ntsc", 4))
drivers/video/hgafb.c +3 -2
diff -ur linux-2.4.4/drivers/video/hgafb.c linux-2.4.4-rs/drivers/video/hgafb.c
@@ -794,8 +794,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt;
-			this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strncmp(this_opt, "font:", 5))
 			strcpy(fb_info.fontname, this_opt+5);
 	}
drivers/video/igafb.c +3 -2
diff -ur linux-2.4.4/drivers/video/igafb.c linux-2.4.4-rs/drivers/video/igafb.c
@@ -773,8 +773,9 @@
     if (!options || !*options)
         return 0;
 
-    for (this_opt = strtok(options, ","); this_opt;
-         this_opt = strtok(NULL, ",")) {
+    while (this_opt = strsep(&options, ",")) {
+	if (!*this_opt)
+		continue;
         if (!strncmp(this_opt, "font:", 5)) {
                 char *p;
                 int i;
drivers/video/imsttfb.c +3 -2
diff -ur linux-2.4.4/drivers/video/imsttfb.c linux-2.4.4-rs/drivers/video/imsttfb.c
@@ -1947,8 +1947,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strncmp(this_opt, "font:", 5)) {
 			char *p;
 			int i;
drivers/video/macfb.c +1 -1
diff -ur linux-2.4.4/drivers/video/macfb.c linux-2.4.4-rs/drivers/video/macfb.c
@@ -848,7 +848,7 @@
 	if (!options || !*options)
 		return;
 	
-	for(this_opt=strtok(options,","); this_opt; this_opt=strtok(NULL,",")) {
+	while (this_opt=strsep(&options,",")) {
 		if (!*this_opt) continue;
 		
 		if (! strcmp(this_opt, "inverse"))
drivers/video/matrox/matroxfb_base.c +1 -1
diff -ur linux-2.4.4/drivers/video/matrox/matroxfb_base.c linux-2.4.4-rs/drivers/video/matrox/matroxfb_base.c
@@ -2348,7 +2348,7 @@
 	if (!options || !*options)
 		return 0;
 
-	for(this_opt=strtok(options,","); this_opt; this_opt=strtok(NULL,",")) {
+	while (this_opt = strsep(&options, ",")) {
 		if (!*this_opt) continue;
 
 		dprintk("matroxfb_setup: option %s\n", this_opt);
drivers/video/platinumfb.c +3 -2
diff -ur linux-2.4.4/drivers/video/platinumfb.c linux-2.4.4-rs/drivers/video/platinumfb.c
@@ -841,8 +841,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strncmp(this_opt, "font:", 5)) {
 			char *p;
 			int i;
drivers/video/retz3fb.c +3 -2
diff -ur linux-2.4.4/drivers/video/retz3fb.c linux-2.4.4-rs/drivers/video/retz3fb.c
@@ -1348,8 +1348,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")){
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strcmp(this_opt, "inverse")) {
 			z3fb_inverse = 1;
 			fb_invert_cmaps();
drivers/video/riva/fbdev.c +3 -2
diff -ur linux-2.4.4/drivers/video/riva/fbdev.c linux-2.4.4-rs/drivers/video/riva/fbdev.c
@@ -2041,8 +2041,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strncmp(this_opt, "font:", 5)) {
 			char *p;
 			int i;
drivers/video/sa1100fb.c +3 -2
diff -ur linux-2.4.4/drivers/video/sa1100fb.c linux-2.4.4-rs/drivers/video/sa1100fb.c
@@ -1282,8 +1282,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+	  if (!*this_opt)
+	    continue;
 
 	  if (!strncmp(this_opt, "bpp:", 4))
  	    current_par.max_bpp = simple_strtoul(this_opt+4, NULL, 0);
drivers/video/sgivwfb.c +3 -2
diff -ur linux-2.4.4/drivers/video/sgivwfb.c linux-2.4.4-rs/drivers/video/sgivwfb.c
@@ -862,8 +862,9 @@
   if (!options || !*options)
     return 0;
 
-  for (this_opt = strtok(options, ","); this_opt;
-       this_opt = strtok(NULL, ",")) {
+  while (this_opt = strsep(&options, ",")) {
+    if (!*this_opt)
+      continue;
     if (!strncmp(this_opt, "font:", 5))
       strcpy(fb_info.fontname, this_opt+5);
   }
drivers/video/sis/sis_main.c +1 -2
diff -ur linux-2.4.4/drivers/video/sis/sis_main.c linux-2.4.4-rs/drivers/video/sis/sis_main.c
@@ -1725,8 +1725,7 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
 		if (!*this_opt)
 			continue;
 
drivers/video/tdfxfb.c +3 -3
diff -ur linux-2.4.4/drivers/video/tdfxfb.c linux-2.4.4-rs/drivers/video/tdfxfb.c
@@ -2061,9 +2061,9 @@
   if(!options || !*options)
     return;
 
-  for(this_opt = strtok(options, ","); 
-      this_opt;
-      this_opt = strtok(NULL, ",")) {
+  while (this_opt = strsep(&options, ",")) {
+    if (!*this_opt)
+      continue;
     if(!strcmp(this_opt, "inverse")) {
       inverse = 1;
       fb_invert_cmaps();
drivers/video/tgafb.c +1 -1
diff -ur linux-2.4.4/drivers/video/tgafb.c linux-2.4.4-rs/drivers/video/tgafb.c
@@ -888,7 +888,7 @@
     int i;
     
     if (options && *options) {
-    	for(this_opt=strtok(options,","); this_opt; this_opt=strtok(NULL,",")) {
+	while (this_opt = strsep(&options, ",")) {
        	    if (!*this_opt) { continue; }
         
 	    if (!strncmp(this_opt, "font:", 5)) {
drivers/video/valkyriefb.c +3 -2
diff -ur linux-2.4.4/drivers/video/valkyriefb.c linux-2.4.4-rs/drivers/video/valkyriefb.c
@@ -801,8 +801,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt;
-	     this_opt = strtok(NULL, ",")) {
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strncmp(this_opt, "font:", 5)) {
 			char *p;
 			int i;
drivers/video/vesafb.c +1 -1
diff -ur linux-2.4.4/drivers/video/vesafb.c linux-2.4.4-rs/drivers/video/vesafb.c
@@ -457,7 +457,7 @@
 	if (!options || !*options)
 		return 0;
 	
-	for(this_opt=strtok(options,","); this_opt; this_opt=strtok(NULL,",")) {
+	while (this_opt = strsep(&options, ",")) {
 		if (!*this_opt) continue;
 		
 		if (! strcmp(this_opt, "inverse"))
drivers/video/vfb.c +3 -2
diff -ur linux-2.4.4/drivers/video/vfb.c linux-2.4.4-rs/drivers/video/vfb.c
@@ -382,8 +382,9 @@
     if (!options || !*options)
 	return 0;
 
-    for (this_opt = strtok(options, ","); this_opt;
-	 this_opt = strtok(NULL, ",")) {
+    while (this_opt = strsep(&options, ",")) {
+	if (!*this_opt)
+	    continue;
 	if (!strncmp(this_opt, "font:", 5))
 	    strcpy(fb_info.fontname, this_opt+5);
     }
drivers/video/vga16fb.c +1 -1
diff -ur linux-2.4.4/drivers/video/vga16fb.c linux-2.4.4-rs/drivers/video/vga16fb.c
@@ -692,7 +692,7 @@
 	if (!options || !*options)
 		return 0;
 	
-	for(this_opt=strtok(options,","); this_opt; this_opt=strtok(NULL,",")) {
+	while (this_opt = strsep(&options, ",")) {
 		if (!*this_opt) continue;
 		
 		if (!strncmp(this_opt, "font:", 5))
drivers/video/virgefb.c +3 -1
diff -ur linux-2.4.4/drivers/video/virgefb.c linux-2.4.4-rs/drivers/video/virgefb.c
@@ -1085,7 +1085,9 @@
 	if (!options || !*options)
 		return 0;
 
-	for (this_opt = strtok(options, ","); this_opt; this_opt = strtok(NULL, ","))
+	while (this_opt = strsep(&options, ",")) {
+		if (!*this_opt)
+			continue;
 		if (!strcmp(this_opt, "inverse")) {
 			Cyberfb_inverse = 1;
 			fb_invert_cmaps();
fs/adfs/super.c +3 -1
diff -ur linux-2.4.4/fs/adfs/super.c linux-2.4.4-rs/fs/adfs/super.c
@@ -171,7 +171,9 @@
 	if (!options)
 		return 0;
 
-	for (opt = strtok(options, ","); opt != NULL; opt = strtok(NULL, ",")) {
+	while (opt = strsep(&options, ",")) {
+		if (!*opt)
+			continue;
 		value = strchr(opt, '=');
 		if (value)
 			*value++ = '\0';
fs/affs/super.c +3 -1
diff -ur linux-2.4.4/fs/affs/super.c linux-2.4.4-rs/fs/affs/super.c
@@ -109,7 +109,9 @@
 	*mount_opts = 0;
 	if (!options)
 		return 1;
-	for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
+	while (this_char = strsep(&options,",")) {
+		if (!*this_char)
+			continue;
 		f = 0;
 		if ((value = strchr(this_char,'=')) != NULL)
 			*value++ = 0;
fs/autofs/inode.c +3 -1
diff -ur linux-2.4.4/fs/autofs/inode.c linux-2.4.4-rs/fs/autofs/inode.c
@@ -60,7 +60,9 @@
 	*pipefd = -1;
 
 	if ( !options ) return 1;
-	for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
+	while (this_char = strsep(&options,",")) {
+		if (!*this_char)
+			continue;
 		if ((value = strchr(this_char,'=')) != NULL)
 			*value++ = 0;
 		if (!strcmp(this_char,"fd")) {
fs/autofs4/inode.c +3 -1
diff -ur linux-2.4.4/fs/autofs4/inode.c linux-2.4.4-rs/fs/autofs4/inode.c
@@ -112,7 +112,9 @@
 	*pipefd = -1;
 
 	if ( !options ) return 1;
-	for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
+	while (this_char = strsep(&options,",")) {
+		if (!*this_char)
+			continue;
 		if ((value = strchr(this_char,'=')) != NULL)
 			*value++ = 0;
 		if (!strcmp(this_char,"fd")) {
fs/ext2/super.c +3 -3
diff -ur linux-2.4.4/fs/ext2/super.c linux-2.4.4-rs/fs/ext2/super.c
@@ -169,9 +169,9 @@
 
 	if (!options)
 		return 1;
-	for (this_char = strtok (options, ",");
-	     this_char != NULL;
-	     this_char = strtok (NULL, ",")) {
+	while (this_char = strsep(&options, ",")) {
+		if (!*this_char)
+			continue;
 		if ((value = strchr (this_char, '=')) != NULL)
 			*value++ = 0;
 		if (!strcmp (this_char, "bsddf"))
fs/hfs/super.c +3 -2
diff -ur linux-2.4.4/fs/hfs/super.c linux-2.4.4-rs/fs/hfs/super.c
@@ -181,8 +181,9 @@
 	if (!options) {
 		goto done;
 	}
-	for (this_char = strtok(options,","); this_char;
-	     this_char = strtok(NULL,",")) {
+	while (this_char = strsep(&options,",")) {
+		if (!*this_char)
+			continue;
 		if ((value = strchr(this_char,'=')) != NULL) {
 			*value++ = 0;
 		}
fs/hpfs/super.c +3 -1
diff -ur linux-2.4.4/fs/hpfs/super.c linux-2.4.4-rs/fs/hpfs/super.c
@@ -176,7 +176,9 @@
 
 	/*printk("Parsing opts: '%s'\n",opts);*/
 
-	for (p = strtok(opts, ","); p != 0; p = strtok(0, ",")) {
+	while (p = strsep(&opts, ",")) {
+		if (!*p)
+			continue;
 		if ((rhs = strchr(p, '=')) != 0)
 			*rhs++ = '\0';
 		if (!strcmp(p, "help")) return 2;
fs/isofs/inode.c +3 -1
diff -ur linux-2.4.4/fs/isofs/inode.c linux-2.4.4-rs/fs/isofs/inode.c
@@ -290,7 +290,9 @@
 	popt->session=-1;
 	popt->sbsector=-1;
 	if (!options) return 1;
-	for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
+	while (this_char = strsep(&options,",")) {
+		if (!*this_char)
+			continue;
 	        if (strncmp(this_char,"norock",6) == 0) {
 		  popt->rock = 'n';
 		  continue;
fs/nfs/nfsroot.c +3 -3
diff -ur linux-2.4.4/fs/nfs/nfsroot.c linux-2.4.4-rs/fs/nfs/nfsroot.c
@@ -202,8 +202,9 @@
 
 	if ((options = strchr(name, ','))) {
 		*options++ = 0;
-		cp = strtok(options, ",");
-		while (cp) {
+		while (cp = strsep(&options, ",")) {
+			if (!*cp)
+				continue;
 			if ((val = strchr(cp, '='))) {
 				struct nfs_int_opts *opts = root_int_opts;
 				*val++ = '\0';
@@ -220,7 +221,6 @@
 					nfs_data.flags |= opts->or_mask;
 				}
 			}
-			cp = strtok(NULL, ",");
 		}
 	}
 	if (name[0] && strcmp(name, "default")) {
fs/proc/inode.c +3 -1
diff -ur linux-2.4.4/fs/proc/inode.c linux-2.4.4-rs/fs/proc/inode.c
@@ -106,7 +106,9 @@
 	*uid = current->uid;
 	*gid = current->gid;
 	if (!options) return 1;
-	for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
+	while (this_char = strsep(&options,",")) {
+		if (!*this_char)
+			continue;
 		if ((value = strchr(this_char,'=')) != NULL)
 			*value++ = 0;
 		if (!strcmp(this_char,"uid")) {
fs/reiserfs/super.c +3 -1
diff -ur linux-2.4.4/fs/reiserfs/super.c linux-2.4.4-rs/fs/reiserfs/super.c
@@ -162,7 +162,9 @@
 	/* use default configuration: create tails, journaling on, no
            conversion to newest format */
 	return 1;
-    for (this_char = strtok (options, ","); this_char != NULL; this_char = strtok (NULL, ",")) {
+    while (this_char = strsep (&options, ",")) {
+	if (!*this_char)
+		continue;
 	if ((value = strchr (this_char, '=')) != NULL)
 	    *value++ = 0;
 	if (!strcmp (this_char, "notail")) {
fs/udf/super.c +3 -1
diff -ur linux-2.4.4/fs/udf/super.c linux-2.4.4-rs/fs/udf/super.c
@@ -213,8 +213,10 @@
 	if (!options)
 		return 1;
 
-	for (opt = strtok(options, ","); opt; opt = strtok(NULL, ","))
+	while (opt = strsep(&options, ","))
 	{
+		if (!*opt)
+			continue;
 		/* Make "opt=val" into two strings */
 		val = strchr(opt, '=');
 		if (val)
fs/ufs/super.c +3 -4
diff -ur linux-2.4.4/fs/ufs/super.c linux-2.4.4-rs/fs/ufs/super.c
@@ -260,10 +260,9 @@
 	if (!options)
 		return 1;
 		
-	for (this_char = strtok (options, ",");
-	     this_char != NULL;
-	     this_char = strtok (NULL, ",")) {
-	     
+	while (this_char = strsep(&options, ",")) {
+		if (!*this_char)
+			continue;
 		if ((value = strchr (this_char, '=')) != NULL)
 			*value++ = 0;
 		if (!strcmp (this_char, "ufstype")) {
#1 lore raw

In message <01050120580701.01713@golmepha> you write:

Quoted 6 lines

Hello,

the patch at the bottom does the bulk job of strtok replacement. It's a very boring patch, containing easy cases, only. It became a bit big, too, but I trust you can digest it nevertheless. It's made against kernel version 2.4.4.

There are two cases where the substitution is problematic:

Array:

	char tmparray[500];
	strcpy(tmparray, str);

	/* for (p = strtok(tmparray, "n"); p; p = strtok(NULL, "n")) { */
	while ((p = strsep(&tmparray, ","))) {

This is clearly wrong, and invokes a compiler warning. &tmparray == tmparray (a cute C oddity I've never really liked). You are blowing away the first few characters in tmparray, and your parser won't work properly.

Dynamic:

	char *tmp = strdup(str);

	/* for (p = strtok(tmp, "n"); p; p = strtok(NULL, "n")) { */
	while ((p = strsep(&tmp, ","))) {
	...
	}

	kfree(tmp);

Here, tmp has changed in the strsep implementation, and kfree will do bad things.

There is a real reason to avoid strtok, and that is SMP and multple threads calling it at once (that said, I don't know of a problem yet). But this patch is a step backwards.

Rusty.
--
Premature optmztion is rt of all evl. --DK

#2 lore raw

Am Freitag, 4. Mai 2001 02:57 schrieb Rusty Russell:

Quoted 2 lines

In message <01050120580701.01713@golmepha> you write:

Quoted 1 line

Hello,

Hi!

Quoted 7 lines
Quoted 5 lines

the patch at the bottom does the bulk job of strtok replacement. It's a very boring patch, containing easy cases, only. It became a bit big, too, but I trust you can digest it nevertheless. It's made against kernel version 2.4.4.

There are two cases where the substitution is problematic:

Yes, but...

The cases which my patch modifies are of a different kind:

	int parse_options(char *options)
	{
		char *p;

		/* for (p = strtok(options, ","); p; p = strtok(NULL, ",")) { */
		while (p = strsep(&options, ",")) {
			/* ... */
		}
		return 0;
	}

No temporary array, no kfree(). Our variable "options" is used for strtsep, only. Notice btw. how we destoy the string content to which "options" points with both strtok and strsep.

That said, it's possible I made a stupid mistake, of course. Or two. Do you agree on the correctness of the code above? If not, forget the whole thing and I'll try again later.

Quoted 27 lines

Array:

	char tmparray[500];
	strcpy(tmparray, str);

	/* for (p = strtok(tmparray, "n"); p; p = strtok(NULL, "n")) { */
	while ((p = strsep(&tmparray, ","))) {

This is clearly wrong, and invokes a compiler warning. &tmparray == tmparray (a cute C oddity I've never really liked). You are blowing away the first few characters in tmparray, and your parser won't work properly.

Dynamic:

	char *tmp = strdup(str);

	/* for (p = strtok(tmp, "n"); p; p = strtok(NULL, "n")) { */
	while ((p = strsep(&tmp, ","))) {
	...
	}

	kfree(tmp);

Here, tmp has changed in the strsep implementation, and kfree will do bad things.

Quoted 7 lines

There is a real reason to avoid strtok, and that is SMP and multple threads calling it at once (that said, I don't know of a problem yet). But this patch is a step backwards.

Rusty.
--
Premature optmztion is rt of all evl. --DK

René

#3 lore raw

In message <01050413055100.00907@golmepha> you write:

Quoted 6 lines

Am Freitag, 4. Mai 2001 02:57 schrieb Rusty Russell:

Quoted 1 line

There are two cases where the substitution is problematic:

Yes, but...

The cases which my patch modifies are of a different kind:

The very first hunk of your patch is wrong. I didn't check the others. Note that the declaration of switches is:

    char switches[len+1];
arch/m68k/atari/config.c +3 -1
@@ -206,13 +206,15 @@
 
     /* parse the options */
-    for( p = strtok( switches, "," ); p; p = strtok( NULL, "," ) ) {
+    while( p = strsep( &switches, "," ) ) {
+	if (!*p)
+		continue;
 	ovsc_shift = 0;
 	if (strncmp( p, "ov_", 3 ) == 0) {
 	    p += 3;
 

Cheers,
Rusty.
--
Premature optmztion is rt of all evl. --DK