zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers@sunsite.dk
Subject: Re: Bug#245974: zsh: export LC_ALL=da_DK causes segfault
Date: Tue, 04 May 2004 16:30:55 +0100	[thread overview]
Message-ID: <7357.1083684655@csr.com> (raw)
In-Reply-To: "Clint Adams"'s message of "Tue, 04 May 2004 11:06:19 EDT." <20040504150619.GA28923@scowler.net>

Clint Adams wrote:
> > This is a better patch, although it's not ideal, owing to the interface
> > of strftime which doesn't signal success or failure properly.
> 
> glibc suggests this method
> 
>           buf[0] = '\1';
>           len = strftime (buf, bufsize, format, tp);
>           if (len == 0 && buf[0] != '\0')
>             {
>               /* Something went wrong in the strftime call.  */
>               ...
>             }
> 
> If it's portable, it would seem a better hack.

I agree, although I don't know if it's portable... the following allows
ztrftime to return -1 for an error.  If the fix isn't portable, it means
we won't handle long strings properly.  I've still limited the size of
the loop in the prompt, since I'm paranoid.  I think making the size
of the buffer depend on the length of the format should also improve
matters there.

Index: Src/prompt.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/prompt.c,v
retrieving revision 1.16
diff -u -r1.16 prompt.c
--- Src/prompt.c	4 May 2004 04:17:29 -0000	1.16
+++ Src/prompt.c	4 May 2004 15:25:22 -0000
@@ -526,11 +526,16 @@
 		    }
 		    timet = time(NULL);
 		    tm = localtime(&timet);
-		    for(t0=80; ; t0*=2) {
+		    /*
+		     * Hack because strftime won't say how
+		     * much space it actually needs.  Try to add it
+		     * a few times until it works.  Some formats don't
+		     * actually have a length, so we could go on for
+		     * ever.
+		     */
+		    for(j = 0, t0 = strlen(tmfmt)*8; j < 3; j++, t0*=2) {
 			addbufspc(t0);
-			if (ztrftime(bp, t0, tmfmt, tm) ||
-			    !strcmp("%P", tmfmt) ||
-			    !strcmp("%p", tmfmt))
+			if (ztrftime(bp, t0, tmfmt, tm) >= 0)
 			    break;
 		    }
 		    bp += strlen(bp);
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.59
diff -u -r1.59 utils.c
--- Src/utils.c	4 May 2004 04:17:29 -0000	1.59
+++ Src/utils.c	4 May 2004 15:25:31 -0000
@@ -1719,6 +1719,11 @@
  * Like the system function, this returns the number of characters
  * copied, not including the terminating NUL.  This may be zero
  * if the string didn't fit.
+ *
+ * As an extension, try to detect an error in strftime --- typically
+ * not enough memory --- and return -1.  Not guaranteed to be portable,
+ * since the strftime() interface doesn't make any guarantees about
+ * the state of the buffer if it returns zero.
  */
 
 /**/
@@ -1831,9 +1836,14 @@
 		 */
 		*buf = '\0';
 		tmp[1] = fmt[-1];
-		if (!strftime(buf, bufsize + 2, tmp, tm) &&
-		    tmp[1]!='p' && tmp[1]!='P')
+		if (!strftime(buf, bufsize + 2, tmp, tm))
+		{
+		    if (*buf) {
+			buf[0] = '\0';
+			return -1;
+		    }
 		    return 0;
+		}
 		decr = strlen(buf);
 		buf += decr;
 		bufsize -= decr - 2;
Index: Src/Modules/datetime.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/datetime.c,v
retrieving revision 1.7
diff -u -r1.7 datetime.c
--- Src/Modules/datetime.c	22 Jan 2004 17:51:06 -0000	1.7
+++ Src/Modules/datetime.c	4 May 2004 15:25:31 -0000
@@ -61,7 +61,7 @@
     buffer = zalloc(bufsize);
 
     for (x=0; x < 4; x++) {
-        if (ztrftime(buffer, bufsize, argv[0], t))
+        if (ztrftime(buffer, bufsize, argv[0], t) >= 0)
 	    break;
 	buffer = zrealloc(buffer, bufsize *= 2);
     }

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


  reply	other threads:[~2004-05-04 15:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20040426144112.87B951C02C@kohn.kiku.dk>
     [not found] ` <20040426154922.GA8375@scowler.net>
     [not found]   ` <20040426201800.GA2029@kohn.kiku.dk>
     [not found]     ` <20040426224229.GA12757@scowler.net>
     [not found]       ` <20040427092356.GA4780@kohn.kiku.dk>
2004-05-03  0:27         ` Clint Adams
2004-05-04  4:14           ` Clint Adams
2004-05-04  9:26             ` Peter Stephenson
2004-05-04 14:41               ` Peter Stephenson
2004-05-04 15:06                 ` Clint Adams
2004-05-04 15:30                   ` Peter Stephenson [this message]
2004-05-05  4:43                     ` Wayne Davison
2004-05-05 11:17                       ` Peter Stephenson
2004-05-04  9:50             ` Oliver Kiddle
2004-05-04 12:11               ` Clint Adams

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=7357.1083684655@csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /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).