Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: ztrftime: more workarounds for broken strftime interface



On Sun, Nov 22, 2015 at 6:27 PM, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> Do we want to try to fix users/20859 ?  It shouldn't be that hard for
> someone in an environment where it fails (I'm having trouble creating
> one).

The problem is that the strftime() function is defined by an idiot. There
is no way to differentiate the success of a zero-length result and any
error. If there is any error then the return result is undefined so we
cannot use it.

I guess we could append some fixed string always, and then discard this
string, for example an x character.

This somehow works and doesn't break the tests, nor my prompt.

---
 Src/utils.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/Src/utils.c b/Src/utils.c
index c19cca8..f42b25b 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -3154,8 +3154,9 @@ strftimehandling:
 		{
 		    int size = fmt - fmtstart;
 		    char *tmp, *last;
-		    tmp = zhalloc(size + 1);
+		    tmp = zhalloc(size + 2);
 		    strncpy(tmp, fmtstart, size);
+		    tmp[size] = 'x';
 		    last = fmt-1;
 		    if (*last == Meta) {
 			/*
@@ -3168,7 +3169,7 @@ strftimehandling:
 			 */
 			*last = *++fmt ^ 32;
 		    }
-		    tmp[size] = '\0';
+		    tmp[size+1] = '\0';
 		    *buf = '\1';
 		    if (!strftime(buf, bufsize + 2, tmp, tm))
 		    {
@@ -3178,7 +3179,7 @@ strftimehandling:
 			}
 			return 0;
 		    }
-		    decr = strlen(buf);
+		    decr = strlen(buf) - 1;
 		    buf += decr;
 		    bufsize -= decr - 2;
 		}
-- 
2.6.1



Messages sorted by: Reverse Date, Date, Thread, Author