1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-10-31 18:10:56 +01:00

19877: improved test for empty strftime strings

This commit is contained in:
Peter Stephenson 2004-05-04 16:43:29 +00:00
parent c98b9dc800
commit f57f65282b
4 changed files with 28 additions and 7 deletions

View file

@ -1,3 +1,9 @@
2004-05-04 Peter Stephenson <pws@csr.com>
* 19877: Src/prompt.c, Src/utils.c, Src/Modules/datetime.c:
Improve 19869: remove infinite loop and attempt to test
for whether strftime() encountered an error.
2004-05-04 Clint Adams <clint@zsh.org>
* 19869: Src/prompt.c, Src/utils.c: avoid segfault when

View file

@ -61,7 +61,7 @@ bin_strftime(char *nam, char **argv, Options ops, int func)
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);
}

View file

@ -526,11 +526,16 @@ putpromptchar(int doprint, int endchar)
}
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);

View file

@ -1719,6 +1719,11 @@ ztrftimebuf(int *bufsizeptr, int decr)
* 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 @@ ztrftime(char *buf, int bufsize, char *fmt, struct tm *tm)
*/
*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;