aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Antipov2012-07-10 10:23:45 +0400
committerDmitry Antipov2012-07-10 10:23:45 +0400
commitc293e30cd9f841e59ce6ae3b2757f0e6daf2e71d (patch)
tree6aaff4b91e43d732c4a7dea94ba3bf28a94b2955
parentaf6e839f9fadf33b22c145d3103f6e3d9656e804 (diff)
downloademacs-c293e30cd9f841e59ce6ae3b2757f0e6daf2e71d.tar.gz
emacs-c293e30cd9f841e59ce6ae3b2757f0e6daf2e71d.zip
Avoid calls to strlen in path processing functions.
* fileio.c (file_name_as_directory): Add comment. Change to add srclen argument and return the length of result. Adjust users accordingly. (directory_file_name): Fix comment. Change to add srclen argument, swap 1nd and 2st arguments to obey the common convention. Adjust users accordingly. * filelock.c (fill_in_lock_file_name): Avoid calls to strlen.
-rw-r--r--src/ChangeLog11
-rw-r--r--src/fileio.c92
-rw-r--r--src/filelock.c5
3 files changed, 61 insertions, 47 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 3b7038ffe49..e54ea7d8fe9 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,14 @@
12012-07-10 Dmitry Antipov <dmantipov@yandex.ru>
2
3 Avoid calls to strlen in path processing functions.
4 * fileio.c (file_name_as_directory): Add comment. Change to add
5 srclen argument and return the length of result. Adjust users
6 accordingly.
7 (directory_file_name): Fix comment. Change to add srclen argument,
8 swap 1nd and 2st arguments to obey the common convention. Adjust
9 users accordingly.
10 * filelock.c (fill_in_lock_file_name): Avoid calls to strlen.
11
12012-07-10 Glenn Morris <rgm@gnu.org> 122012-07-10 Glenn Morris <rgm@gnu.org>
2 13
3 * s/irix6-5.h (SETUP_SLAVE_PTY, PTY_NAME_SPRINTF): Drop ifdef guards. 14 * s/irix6-5.h (SETUP_SLAVE_PTY, PTY_NAME_SPRINTF): Drop ifdef guards.
diff --git a/src/fileio.c b/src/fileio.c
index 8f3b9e92257..532ab6097e5 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -453,32 +453,33 @@ get a current directory to run processes in. */)
453 return Ffile_name_directory (filename); 453 return Ffile_name_directory (filename);
454} 454}
455 455
456 456/* Convert from file name SRC of length SRCLEN to directory name
457static char * 457 in DST. On UNIX, just make sure there is a terminating /.
458file_name_as_directory (char *out, const char *in) 458 Return the length of DST. */
459{
460 ptrdiff_t len = strlen (in);
461 459
462 if (len == 0) 460static ptrdiff_t
461file_name_as_directory (char *dst, const char *src, ptrdiff_t srclen)
462{
463 if (srclen == 0)
463 { 464 {
464 out[0] = '.'; 465 dst[0] = '.';
465 out[1] = '/'; 466 dst[1] = '/';
466 out[2] = 0; 467 dst[2] = '\0';
467 return out; 468 return 2;
468 } 469 }
469 470
470 strcpy (out, in); 471 strcpy (dst, src);
471 472
472 /* For Unix syntax, Append a slash if necessary */ 473 if (!IS_DIRECTORY_SEP (dst[srclen - 1]))
473 if (!IS_DIRECTORY_SEP (out[len - 1]))
474 { 474 {
475 out[len] = DIRECTORY_SEP; 475 dst[srclen] = DIRECTORY_SEP;
476 out[len + 1] = '\0'; 476 dst[srclen + 1] = '\0';
477 srclen++;
477 } 478 }
478#ifdef DOS_NT 479#ifdef DOS_NT
479 dostounix_filename (out); 480 dostounix_filename (dst);
480#endif 481#endif
481 return out; 482 return srclen;
482} 483}
483 484
484DEFUN ("file-name-as-directory", Ffile_name_as_directory, 485DEFUN ("file-name-as-directory", Ffile_name_as_directory,
@@ -492,6 +493,7 @@ For a Unix-syntax file name, just appends a slash. */)
492 (Lisp_Object file) 493 (Lisp_Object file)
493{ 494{
494 char *buf; 495 char *buf;
496 ptrdiff_t length;
495 Lisp_Object handler; 497 Lisp_Object handler;
496 498
497 CHECK_STRING (file); 499 CHECK_STRING (file);
@@ -511,39 +513,34 @@ For a Unix-syntax file name, just appends a slash. */)
511 } 513 }
512 514
513 buf = alloca (SBYTES (file) + 10); 515 buf = alloca (SBYTES (file) + 10);
514 file_name_as_directory (buf, SSDATA (file)); 516 length = file_name_as_directory (buf, SSDATA (file), SBYTES (file));
515 return make_specified_string (buf, -1, strlen (buf), 517 return make_specified_string (buf, -1, length, STRING_MULTIBYTE (file));
516 STRING_MULTIBYTE (file));
517} 518}
518 519
519/* 520/* Convert from directory name SRC of length SRCLEN to
520 * Convert from directory name to filename. 521 file name in DST. On UNIX, just make sure there isn't
521 * On UNIX, it's simple: just make sure there isn't a terminating / 522 a terminating /. Return the length of DST. */
522 523
523 * Value is nonzero if the string output is different from the input. 524static ptrdiff_t
524 */ 525directory_file_name (char *dst, char *src, ptrdiff_t srclen)
525
526static int
527directory_file_name (char *src, char *dst)
528{ 526{
529 ptrdiff_t slen;
530
531 slen = strlen (src);
532
533 /* Process as Unix format: just remove any final slash. 527 /* Process as Unix format: just remove any final slash.
534 But leave "/" unchanged; do not change it to "". */ 528 But leave "/" unchanged; do not change it to "". */
535 strcpy (dst, src); 529 strcpy (dst, src);
536 if (slen > 1 530 if (srclen > 1
537 && IS_DIRECTORY_SEP (dst[slen - 1]) 531 && IS_DIRECTORY_SEP (dst[srclen - 1])
538#ifdef DOS_NT 532#ifdef DOS_NT
539 && !IS_ANY_SEP (dst[slen - 2]) 533 && !IS_ANY_SEP (dst[srclen - 2])
540#endif 534#endif
541 ) 535 )
542 dst[slen - 1] = 0; 536 {
537 dst[srclen - 1] = 0;
538 srclen--;
539 }
543#ifdef DOS_NT 540#ifdef DOS_NT
544 dostounix_filename (dst); 541 dostounix_filename (dst);
545#endif 542#endif
546 return 1; 543 return srclen;
547} 544}
548 545
549DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name, 546DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name,
@@ -556,6 +553,7 @@ In Unix-syntax, this function just removes the final slash. */)
556 (Lisp_Object directory) 553 (Lisp_Object directory)
557{ 554{
558 char *buf; 555 char *buf;
556 ptrdiff_t length;
559 Lisp_Object handler; 557 Lisp_Object handler;
560 558
561 CHECK_STRING (directory); 559 CHECK_STRING (directory);
@@ -576,9 +574,8 @@ In Unix-syntax, this function just removes the final slash. */)
576 } 574 }
577 575
578 buf = alloca (SBYTES (directory) + 20); 576 buf = alloca (SBYTES (directory) + 20);
579 directory_file_name (SSDATA (directory), buf); 577 length = directory_file_name (buf, SSDATA (directory), SBYTES (directory));
580 return make_specified_string (buf, -1, strlen (buf), 578 return make_specified_string (buf, -1, length, STRING_MULTIBYTE (directory));
581 STRING_MULTIBYTE (directory));
582} 579}
583 580
584static const char make_temp_name_tbl[64] = 581static const char make_temp_name_tbl[64] =
@@ -1130,8 +1127,9 @@ filesystem tree, not (expand-file-name ".." dirname). */)
1130 } 1127 }
1131 if (!IS_DIRECTORY_SEP (nm[0])) 1128 if (!IS_DIRECTORY_SEP (nm[0]))
1132 { 1129 {
1133 char * tmp = alloca (strlen (newdir) + strlen (nm) + 2); 1130 ptrdiff_t newlen = strlen (newdir);
1134 file_name_as_directory (tmp, newdir); 1131 char *tmp = alloca (newlen + strlen (nm) + 2);
1132 file_name_as_directory (tmp, newdir, newlen);
1135 strcat (tmp, nm); 1133 strcat (tmp, nm);
1136 nm = tmp; 1134 nm = tmp;
1137 } 1135 }
@@ -1180,6 +1178,7 @@ filesystem tree, not (expand-file-name ".." dirname). */)
1180 /* Get rid of any slash at the end of newdir, unless newdir is 1178 /* Get rid of any slash at the end of newdir, unless newdir is
1181 just / or // (an incomplete UNC name). */ 1179 just / or // (an incomplete UNC name). */
1182 length = strlen (newdir); 1180 length = strlen (newdir);
1181 tlen = length + 1;
1183 if (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1]) 1182 if (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1])
1184#ifdef WINDOWSNT 1183#ifdef WINDOWSNT
1185 && !(length == 2 && IS_DIRECTORY_SEP (newdir[0])) 1184 && !(length == 2 && IS_DIRECTORY_SEP (newdir[0]))
@@ -1189,12 +1188,15 @@ filesystem tree, not (expand-file-name ".." dirname). */)
1189 char *temp = alloca (length); 1188 char *temp = alloca (length);
1190 memcpy (temp, newdir, length - 1); 1189 memcpy (temp, newdir, length - 1);
1191 temp[length - 1] = 0; 1190 temp[length - 1] = 0;
1191 length--;
1192 newdir = temp; 1192 newdir = temp;
1193 } 1193 }
1194 tlen = length + 1;
1195 } 1194 }
1196 else 1195 else
1197 tlen = 0; 1196 {
1197 length = 0;
1198 tlen = 0;
1199 }
1198 1200
1199 /* Now concatenate the directory and name to new space in the stack frame. */ 1201 /* Now concatenate the directory and name to new space in the stack frame. */
1200 tlen += strlen (nm) + 1; 1202 tlen += strlen (nm) + 1;
@@ -1225,7 +1227,7 @@ filesystem tree, not (expand-file-name ".." dirname). */)
1225 strcpy (target, newdir); 1227 strcpy (target, newdir);
1226 } 1228 }
1227 else 1229 else
1228 file_name_as_directory (target, newdir); 1230 file_name_as_directory (target, newdir, length);
1229 } 1231 }
1230 1232
1231 strcat (target, nm); 1233 strcat (target, nm);
diff --git a/src/filelock.c b/src/filelock.c
index 30258a5ffa0..e5de7355652 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -300,6 +300,7 @@ typedef struct
300static void 300static void
301fill_in_lock_file_name (register char *lockfile, register Lisp_Object fn) 301fill_in_lock_file_name (register char *lockfile, register Lisp_Object fn)
302{ 302{
303 ptrdiff_t length = SBYTES (fn);
303 register char *p; 304 register char *p;
304 struct stat st; 305 struct stat st;
305 int count = 0; 306 int count = 0;
@@ -309,14 +310,14 @@ fill_in_lock_file_name (register char *lockfile, register Lisp_Object fn)
309 /* Shift the nondirectory part of the file name (including the null) 310 /* Shift the nondirectory part of the file name (including the null)
310 right two characters. Here is one of the places where we'd have to 311 right two characters. Here is one of the places where we'd have to
311 do something to support 14-character-max file names. */ 312 do something to support 14-character-max file names. */
312 for (p = lockfile + strlen (lockfile); p != lockfile && *p != '/'; p--) 313 for (p = lockfile + length; p != lockfile && *p != '/'; p--)
313 p[2] = *p; 314 p[2] = *p;
314 315
315 /* Insert the `.#'. */ 316 /* Insert the `.#'. */
316 p[1] = '.'; 317 p[1] = '.';
317 p[2] = '#'; 318 p[2] = '#';
318 319
319 p = p + strlen (p); 320 p = p + length + 2;
320 321
321 while (lstat (lockfile, &st) == 0 && !S_ISLNK (st.st_mode)) 322 while (lstat (lockfile, &st) == 0 && !S_ISLNK (st.st_mode))
322 { 323 {