{"thread":{"id":"48085","subject":"[PATCH] mingw: abort on invalid strftime formats","startedAt":"2018-03-19T16:49:52Z","lastAt":"2018-03-20T13:58:21Z","messageCount":3,"participants":["Johannes Schindelin","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"342200","messageId":"66189602d022dd1911b5f5d98594506b80bb81ff.1521478106.git.johannes.schindelin@gmx.de","threadId":"48085","inReplyTo":null,"subject":"[PATCH] mingw: abort on invalid strftime formats","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-03-19T16:49:22Z","receivedAt":"2018-03-19T16:49:52Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"On Windows, strftime() does not silently ignore invalid formats, but\nwarns about them and then returns 0 and sets errno to EINVAL.\n\nUnfortunately, Git does not expect such a behavior, as it disagrees\nwith strftime()'s semantics on Linux. As a consequence, Git\nmisinterprets the return value 0 as \"I need more space\" and grows the\nbuffer. As the larger buffer does not fix the format, the buffer grows\nand grows and grows until we are out of memory and abort.\n\nIdeally, we would switch off the parameter validation just for\nstrftime(), but we cannot even override the invalid parameter handler\nvia _set_thread_local_invalid_parameter_handler() using MINGW because\nthat function is not declared. Even _set_invalid_parameter_handler(),\nwhich *is* declared, does not help, as it simply does... nothing.\n\nSo let's just bite the bullet and override strftime() for MINGW and\nabort on an invalid format string. While this does not provide the\nbest user experience, it is the best we can do.\n\nSee https://msdn.microsoft.com/en-us/library/fe06s4ak.aspx for more\ndetails.\n\nThis fixes https://github.com/git-for-windows/git/issues/863\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n\n\tThis is a really old patch (from 2016) that I had not managed to\n\tcontribute to git.git yet...\n\n compat/mingw.c | 11 +++++++++++\n compat/mingw.h |  3 +++\n 2 files changed, 14 insertions(+)\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex 2d44d21aca8..a67872babf3 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -761,6 +761,17 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)\n \treturn rc;\n }\n \n+#undef strftime\n+size_t mingw_strftime(char *s, size_t max,\n+\t\t      const char *format, const struct tm *tm)\n+{\n+\tsize_t ret = strftime(s, max, format, tm);\n+\n+\tif (!ret && errno == EINVAL)\n+\t\tdie(\"invalid strftime format: '%s'\", format);\n+\treturn ret;\n+}\n+\n unsigned int sleep (unsigned int seconds)\n {\n \tSleep(seconds*1000);\ndiff --git a/compat/mingw.h b/compat/mingw.h\nindex e03aecfe2e6..571019d0bdd 100644\n--- a/compat/mingw.h\n+++ b/compat/mingw.h\n@@ -361,6 +361,9 @@ int mingw_fstat(int fd, struct stat *buf);\n \n int mingw_utime(const char *file_name, const struct utimbuf *times);\n #define utime mingw_utime\n+size_t mingw_strftime(char *s, size_t max,\n+\t\t   const char *format, const struct tm *tm);\n+#define strftime mingw_strftime\n \n pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,\n \t\t     const char *dir,\n\nbase-commit: 0afbf6caa5b16dcfa3074982e5b48e27d452dbbb\n-- \n2.16.1.windows.4\n\nPublished-As: https://github.com/dscho/git/releases/tag/mingw-strftime-v1\nFetch-It-Via: git fetch https://github.com/dscho/git mingw-strftime-v1\n"},{"id":"342217","messageId":"xmqqy3iouf4b.fsf@gitster-ct.c.googlers.com","threadId":"48085","inReplyTo":"66189602d022dd1911b5f5d98594506b80bb81ff.1521478106.git.johannes.schindelin@gmx.de","subject":"Re: [PATCH] mingw: abort on invalid strftime formats","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2018-03-19T17:52:52Z","receivedAt":"2018-03-19T17:54:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <johannes.schindelin@gmx.de> writes:\n\n> On Windows, strftime() does not silently ignore invalid formats, but\n> warns about them and then returns 0 and sets errno to EINVAL.\n>\n> Unfortunately, Git does not expect such a behavior, as it disagrees\n> with strftime()'s semantics on Linux. As a consequence, Git\n> misinterprets the return value 0 as \"I need more space\" and grows the\n> buffer. As the larger buffer does not fix the format, the buffer grows\n> and grows and grows until we are out of memory and abort.\n\nYuck, it is bad that the callers assume 0 is always \"need more\nspace\", as \"If a conversion specification does not correspond to any\nof the above, the behavior is undefined.\" is what we get from POSIX.\n\n> So let's just bite the bullet and override strftime() for MINGW and\n> abort on an invalid format string. While this does not provide the\n> best user experience, it is the best we can do.\n\nAs long as we allow arbitrary end-user input passed to strftime(),\nthis is probably a step in the right direction.  \n\nAs to the future direction, I however wonder if we can return an\nerror from here and make the caller cooperate a bit more.  Of\ncourse, implementation of strftime() that silently ignore invalid\nformats would not be able to return correct errors like an updated\nmingw_strftime() that does not die but communicates error to its\ncaller would, though.\n\nMy \"git grep\" is hitting only one caller of strftime(), which is\nstrbuf_addftime(), which already does some magic to the format\nstring, so such a future enhancement may not be _so_ bad.\n\nWill apply, thanks.  I do not think there is no reason to cook this\nin 'next', and would assume this can instead go directly to 'master'\nto be part of v2.17-rc1 and onward, right?\n"},{"id":"342317","messageId":"nycvar.QRO.7.76.6.1803201444440.55@ZVAVAG-6OXH6DA.rhebcr.pbec.zvpebfbsg.pbz","threadId":"48085","inReplyTo":"xmqqy3iouf4b.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH] mingw: abort on invalid strftime formats","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2018-03-20T13:57:58Z","receivedAt":"2018-03-20T13:58:21Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Junio,\n\nOn Mon, 19 Mar 2018, Junio C Hamano wrote:\n\n> Johannes Schindelin <johannes.schindelin@gmx.de> writes:\n> \n> > On Windows, strftime() does not silently ignore invalid formats, but\n> > warns about them and then returns 0 and sets errno to EINVAL.\n> >\n> > Unfortunately, Git does not expect such a behavior, as it disagrees\n> > with strftime()'s semantics on Linux. As a consequence, Git\n> > misinterprets the return value 0 as \"I need more space\" and grows the\n> > buffer. As the larger buffer does not fix the format, the buffer grows\n> > and grows and grows until we are out of memory and abort.\n> \n> Yuck, it is bad that the callers assume 0 is always \"need more space\",\n> as \"If a conversion specification does not correspond to any of the\n> above, the behavior is undefined.\" is what we get from POSIX.\n\nYeah, well, our own rules state that we are sometimes stricter than POSIX\nwhen it is pragmatic. This is one of those cases, methinks.\n\n> > So let's just bite the bullet and override strftime() for MINGW and\n> > abort on an invalid format string. While this does not provide the\n> > best user experience, it is the best we can do.\n> \n> As long as we allow arbitrary end-user input passed to strftime(), this\n> is probably a step in the right direction.  \n> \n> As to the future direction, I however wonder if we can return an error\n> from here and make the caller cooperate a bit more.  Of course,\n> implementation of strftime() that silently ignore invalid formats would\n> not be able to return correct errors like an updated mingw_strftime()\n> that does not die but communicates error to its caller would, though.\n\nAnd I would not know what the caller should do in that case, quite\nhonestly... Would strftime() somehow tell us *which* placeholder it took\noffense with? And would we \"quote\" that?\n\n> My \"git grep\" is hitting only one caller of strftime(), which is\n> strbuf_addftime(), which already does some magic to the format\n> string, so such a future enhancement may not be _so_ bad.\n\nRight, except that I do not think I could get the exact error condition\nnecessary to know *how* to munge the format further, not unless I invest a\nserious amount of work in it ;-)\n\n> Will apply, thanks.  I do not think there is no reason to cook this\n> in 'next', and would assume this can instead go directly to 'master'\n> to be part of v2.17-rc1 and onward, right?\n\nThanks. Given that this patch has been in Git for Windows for quite a\nwhile without problems, I think it is safe to get it directly into\n`master`. FWIW this late in the -rc phase, I would be very reluctant to\nsend anything I deem unworthy of `master` anyway.\n\nCiao,\nDscho\n"}]}