zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Subject: [PATCH 3/3] Clean up chabspath() [':a' word modifier].
Date: Fri, 10 Jun 2016 17:36:24 +0000	[thread overview]
Message-ID: <1465580184-3095-3-git-send-email-danielsh@tarsus.local2> (raw)
In-Reply-To: <1465580184-3095-1-git-send-email-danielsh@tarsus.local2>

Fail fast when the first character isn't "/", and use that to simplify the
'..' handling.

The incumbent code was subtly wrong in various ways (for example,
the first 'else if' branch didn't check that dest[-4] was a slash).
---
 Src/hist.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/Src/hist.c b/Src/hist.c
index 07f56b0..2cce970 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -1767,6 +1767,10 @@ chabspath(char **junkptr)
     if (**junkptr != '/') {
 	*junkptr = zhtricat(metafy(zgetcwd(), -1, META_HEAPDUP), "/", *junkptr);
     }
+    if (**junkptr != '/') {
+	/* Can happen after 'rmdir $PWD; zsh' */
+	return 0;
+    }
 
     current = *junkptr;
     dest = *junkptr;
@@ -1783,7 +1787,9 @@ chabspath(char **junkptr)
 
     for (;;) {
 	if (*current == '/') {
+	    /* Contract multiple slashes. */
 #ifdef __CYGWIN__
+	    /* Exception: on Cygwin, two slashes at the very start are significant. */
 	    if (current == *junkptr && current[1] == '/')
 		*dest++ = *current++;
 #endif
@@ -1791,28 +1797,17 @@ chabspath(char **junkptr)
 	    while (*current == '/')
 		current++;
 	} else if (!*current) {
+	    /* Remove trailing slashes. */
 	    while (dest > *junkptr + 1 && dest[-1] == '/')
 		dest--;
 	    *dest = '\0';
 	    break;
 	} else if (current[0] == '.' && current[1] == '.' &&
 		   (!current[2] || current[2] == '/')) {
-		if (current == *junkptr || dest == *junkptr) {
-		    *dest++ = '.';
-		    *dest++ = '.';
-		    current += 2;
-		} else if (dest > *junkptr + 2 &&
-			   !strncmp(dest - 3, "../", 3)) {
-		    *dest++ = '.';
-		    *dest++ = '.';
-		    current += 2;
-		} else if (dest > *junkptr + 1) {
-		    *dest = '\0';
+		if (dest > *junkptr + 1) {
 		    for (dest--;
-			 dest > *junkptr + 1 && dest[-1] != '/';
+			 dest[-1] != '/';
 			 dest--);
-		    if (dest[-1] != '/')
-			dest--;
 		    current += 2;
 		    if (*current == '/')
 			current++;


      parent reply	other threads:[~2016-06-10 17:43 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-10 17:36 [PATCH 1/3] Tests: Add tests for the ':a' and ':A' modifiers Daniel Shahaf
2016-06-10 17:36 ` [PATCH 2/3] Fix the ':A' word modifier on paths with '..' components Daniel Shahaf
2016-06-10 18:54   ` Mikael Magnusson
2016-06-10 19:46     ` Bart Schaefer
2016-06-12 14:28       ` Daniel Shahaf
2016-06-12 16:49         ` Bart Schaefer
2016-06-13  8:52           ` Daniel Shahaf
2016-06-21  1:53             ` [PATCH v2 1/3] Tests: Add tests for the ':a' and ':A' modifiers Daniel Shahaf
2016-06-21  1:53               ` [PATCH v2 2/3] Fix the ':A' word modifier on paths with '..' components Daniel Shahaf
2016-06-21  3:08                 ` Mikael Magnusson
2016-06-25 16:28                   ` Daniel Shahaf
2016-06-25 16:47                     ` Mikael Magnusson
2016-06-27  0:20                       ` Daniel Shahaf
2016-06-28 14:48                         ` Bart Schaefer
2016-07-01  5:11                           ` Daniel Shahaf
2016-07-01 16:05                             ` Bart Schaefer
2016-07-03 20:20                               ` Peter Stephenson
2016-07-05  4:57                               ` Daniel Shahaf
2016-06-21  1:53               ` [PATCH v2 3/3] Clean up chabspath() [':a' word modifier] Daniel Shahaf
2016-06-10 17:36 ` Daniel Shahaf [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1465580184-3095-3-git-send-email-danielsh@tarsus.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).