{"thread":{"id":"18221","subject":"[PATCH] grep: make show_line more portable","startedAt":"2009-03-09T01:15:26Z","lastAt":"2009-03-09T19:34:03Z","messageCount":8,"participants":["Brian Gernhardt","Junio C Hamano","Jay Soffian","Johannes Schindelin","René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"107422","messageId":"1236561326-1231-1-git-send-email-benji@silverinsanity.com","threadId":"18221","inReplyTo":null,"subject":"[PATCH] grep: make show_line more portable","fromName":"Brian Gernhardt","fromEmail":"benji@silverinsanity.com","sentAt":"2009-03-09T01:15:26Z","receivedAt":"2009-03-09T01:15:26Z","isPatch":true,"sender":{"key":"benji@silverinsanity.com","avatar":"https://gravatar.com/avatar/e06c101dbc25c68114d859b4a9ec7cf8a2c52fd2b0270ef0eac0e2e63ff22311?d=mp&s=160"},"body":"On OS X the printf specifier \"%.0s\" outputs the entire string instead\nof 0 characters as POSIX states.\n\nIn addition, for * width or precision printf expects an integer\nargument.  On systems were regoff_t is 64-bit, unexpected results can\noccur.\n\nTo fix these, use if statements to catch 0 precisions and casts to\nconvert regoff_t to int.\n\nSigned-off-by: Brian Gernhardt <benji@silverinsanity.com>\n---\n grep.c |   16 ++++++++++------\n 1 files changed, 10 insertions(+), 6 deletions(-)\n\ndiff --git a/grep.c b/grep.c\nindex cace1c8..ec68200 100644\n--- a/grep.c\n+++ b/grep.c\n@@ -489,18 +489,22 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,\n \n \t\t*eol = '\\0';\n \t\twhile (next_match(opt, bol, eol, ctx, &match, eflags)) {\n-\t\t\tprintf(\"%.*s%s%.*s%s\",\n-\t\t\t       match.rm_so, bol,\n-\t\t\t       opt->color_match,\n-\t\t\t       match.rm_eo - match.rm_so, bol + match.rm_so,\n-\t\t\t       GIT_COLOR_RESET);\n+\t\t\tif( match.rm_so > 0 )\n+\t\t\t\tprintf( \"%.*s\", (int) match.rm_so, bol );\n+\t\t\tif( match.rm_eo > match.rm_so )\n+\t\t\t\tprintf(\"%s%.*s%s\",\n+\t\t\t\t\t   opt->color_match,\n+\t\t\t\t\t  (int) (match.rm_eo - match.rm_so), bol + match.rm_so,\n+\t\t\t\t\t   GIT_COLOR_RESET);\n \t\t\tbol += match.rm_eo;\n \t\t\trest -= match.rm_eo;\n \t\t\teflags = REG_NOTBOL;\n \t\t}\n \t\t*eol = ch;\n \t}\n-\tprintf(\"%.*s\\n\", rest, bol);\n+\tif( rest > 0 )\n+\t\tprintf(\"%.*s\", rest, bol);\n+\tprintf(\"\\n\");\n }\n \n static int grep_buffer_1(struct grep_opt *opt, const char *name,\n-- \n1.6.2.222.g01cbd\n"},{"id":"107427","messageId":"7vhc23iil4.fsf@gitster.siamese.dyndns.org","threadId":"18221","inReplyTo":"1236561326-1231-1-git-send-email-benji@silverinsanity.com","subject":"Re: [PATCH] grep: make show_line more portable","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-09T01:35:35Z","receivedAt":"2009-03-09T01:35:35Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Brian Gernhardt <benji@silverinsanity.com> writes:\n\n> On OS X the printf specifier \"%.0s\" outputs the entire string instead\n> of 0 characters as POSIX states.\n>\n> In addition, for * width or precision printf expects an integer\n> argument.  On systems were regoff_t is 64-bit, unexpected results can\n> occur.\n\nI would prefer to see these two issues solved as separate issues.\n\nSpecifically, I'd like to know if the patch from me to you a few message\nago solves the issue.\n\nIf you still need a \"some implementations of printf is broken with respect\nto 0 precision\" workaround on top of that patch, we would want to add it\nseparately, but it may have to cover not just this printf(), as I am not\nconvinced this is the only place that lets (integer) 0 passed to the\n\"%.*s\" format.  That patch needs to be written after a separate auditing\nof output from \"git grep -n -e 'printf.*%\\.\\*s'\", which I do not think\nhappened yet (at least I haven't done that, and I somehow do not think you\nhave yet either).\n"},{"id":"107431","messageId":"76718490903081922p105ebf79vb0bf06989413887c@mail.gmail.com","threadId":"18221","inReplyTo":"1236561326-1231-1-git-send-email-benji@silverinsanity.com","subject":"Re: [PATCH] grep: make show_line more portable","fromName":"Jay Soffian","fromEmail":"jaysoffian@gmail.com","sentAt":"2009-03-09T02:22:07Z","receivedAt":"2009-03-09T02:22:07Z","isPatch":true,"sender":{"key":"jaysoffian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155970?v=4"},"body":"On Sun, Mar 8, 2009 at 9:15 PM, Brian Gernhardt\n<benji@silverinsanity.com> wrote:\n> On OS X the printf specifier \"%.0s\" outputs the entire string instead\n> of 0 characters as POSIX states.\n\nDoes not reproduce for me:\n\n$ cat foo.c && gcc -m64 foo.c -o foo32 && gcc foo.c -o foo64 && file\nfoo32 foo64 && ./foo32 && ./foo64\n#include \"stdio.h\"\n#include \"stdlib.h\"\nmain() {\n\tprintf(\"1 '%.0s'\\n\", \"foobar\");\n\tprintf(\"2 '%.*s'\\n\", 0, \"foobar\");\n\texit(0);\n}\nfoo32: Mach-O 64-bit executable x86_64\nfoo64: Mach-O executable i386\n1 ''\n2 ''\n1 ''\n2 ''\n\nOS X 10.5.6 (Darwin 9.6.0). i686-apple-darwin9-gcc-4.0.1. Same linkage for both:\n\n/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)\n/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current\nversion 111.1.3)\n\nj.\n"},{"id":"107432","messageId":"76718490903081923w3779d6aeu4081022a6630eb73@mail.gmail.com","threadId":"18221","inReplyTo":"76718490903081922p105ebf79vb0bf06989413887c@mail.gmail.com","subject":"Re: [PATCH] grep: make show_line more portable","fromName":"Jay Soffian","fromEmail":"jaysoffian@gmail.com","sentAt":"2009-03-09T02:23:36Z","receivedAt":"2009-03-09T02:23:36Z","isPatch":true,"sender":{"key":"jaysoffian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155970?v=4"},"body":"On Sun, Mar 8, 2009 at 10:22 PM, Jay Soffian <jaysoffian@gmail.com> wrote:\n> foo32: Mach-O 64-bit executable x86_64\n> foo64: Mach-O executable i386\n\nOkay, so I may be brain-damaged in my naming, but that doesn't\ninvalidate the results. :-)\n\nj.\n"},{"id":"107433","messageId":"5ACAF49A-84B5-4F55-A8B8-0FC711708810@silverinsanity.com","threadId":"18221","inReplyTo":"76718490903081922p105ebf79vb0bf06989413887c@mail.gmail.com","subject":"Re: [PATCH] grep: make show_line more portable","fromName":"Brian Gernhardt","fromEmail":"benji@silverinsanity.com","sentAt":"2009-03-09T02:44:40Z","receivedAt":"2009-03-09T02:44:40Z","isPatch":true,"sender":{"key":"benji@silverinsanity.com","avatar":"https://gravatar.com/avatar/e06c101dbc25c68114d859b4a9ec7cf8a2c52fd2b0270ef0eac0e2e63ff22311?d=mp&s=160"},"body":"\nOn Mar 8, 2009, at 10:22 PM, Jay Soffian wrote:\n\n> On Sun, Mar 8, 2009 at 9:15 PM, Brian Gernhardt\n> <benji@silverinsanity.com> wrote:\n>> On OS X the printf specifier \"%.0s\" outputs the entire string instead\n>> of 0 characters as POSIX states.\n>\n> Does not reproduce for me:\n\nNor for me, as I noted on the other thread...  And looking again, I  \nwas reading the man page for printf(1), not printf(3).  Ouch.   \n*grumble, grumble*  I'm crawling back under my rock now.\n\n~~ B\n"},{"id":"107437","messageId":"7vbpsbic85.fsf@gitster.siamese.dyndns.org","threadId":"18221","inReplyTo":"5ACAF49A-84B5-4F55-A8B8-0FC711708810@silverinsanity.com","subject":"Re: [PATCH] grep: make show_line more portable","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-09T03:52:58Z","receivedAt":"2009-03-09T03:52:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Brian Gernhardt <benji@silverinsanity.com> writes:\n\n> On Mar 8, 2009, at 10:22 PM, Jay Soffian wrote:\n>\n>> On Sun, Mar 8, 2009 at 9:15 PM, Brian Gernhardt\n>> <benji@silverinsanity.com> wrote:\n>>> On OS X the printf specifier \"%.0s\" outputs the entire string instead\n>>> of 0 characters as POSIX states.\n>>\n>> Does not reproduce for me:\n>\n> Nor for me, as I noted on the other thread...  And looking again, I\n> was reading the man page for printf(1), not printf(3).  Ouch.\n> *grumble, grumble*  I'm crawling back under my rock now.\n\nHeh, people make mistakes and others are here to help spot them.\nCollectively we all win.\n\nThanks for a breakage report, initial fix and a confirmation.\n"},{"id":"107444","messageId":"alpine.DEB.1.00.0903091049150.10279@pacific.mpi-cbg.de","threadId":"18221","inReplyTo":"7vbpsbic85.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] grep: make show_line more portable","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-03-09T09:50:40Z","receivedAt":"2009-03-09T09:50:40Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sun, 8 Mar 2009, Junio C Hamano wrote:\n\n> Brian Gernhardt <benji@silverinsanity.com> writes:\n> \n> > On Mar 8, 2009, at 10:22 PM, Jay Soffian wrote:\n> >\n> >> On Sun, Mar 8, 2009 at 9:15 PM, Brian Gernhardt\n> >> <benji@silverinsanity.com> wrote:\n> >>> On OS X the printf specifier \"%.0s\" outputs the entire string instead\n> >>> of 0 characters as POSIX states.\n> >>\n> >> Does not reproduce for me:\n> >\n> > Nor for me, as I noted on the other thread...  And looking again, I\n> > was reading the man page for printf(1), not printf(3).  Ouch.\n> > *grumble, grumble*  I'm crawling back under my rock now.\n> \n> Heh, people make mistakes and others are here to help spot them.\n> Collectively we all win.\n\nOne of my favorite quotes these days:\n\nThe computer \"doth make fools of us all,\" so that any fool without the \nability to share a laugh on himself will be unable to tolerate programming \nfor long. ''(Gerald M. Weinberg)''\n\n> Thanks for a breakage report, initial fix and a confirmation.\n\nYes, I think this discussion was valuable, not only because it fixed a \nbug, but also because I learnt that %.*s with a negative length defaults \nto the total string.\n\nCiao,\nDscho\n"},{"id":"107494","messageId":"49B56F2B.1080402@lsrfire.ath.cx","threadId":"18221","inReplyTo":"5ACAF49A-84B5-4F55-A8B8-0FC711708810@silverinsanity.com","subject":"Re: [PATCH] grep: make show_line more portable","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-03-09T19:34:03Z","receivedAt":"2009-03-09T19:34:03Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Brian Gernhardt schrieb:\n> \n> On Mar 8, 2009, at 10:22 PM, Jay Soffian wrote:\n> \n>> On Sun, Mar 8, 2009 at 9:15 PM, Brian Gernhardt\n>> <benji@silverinsanity.com> wrote:\n>>> On OS X the printf specifier \"%.0s\" outputs the entire string instead\n>>> of 0 characters as POSIX states.\n>>\n>> Does not reproduce for me:\n> \n> Nor for me, as I noted on the other thread...  And looking again, I was\n> reading the man page for printf(1), not printf(3).  Ouch.  *grumble,\n> grumble*  I'm crawling back under my rock now.\n\nSorry for introducing a Linuxism. :-/  Thanks for testing and reporting\nand for fixing the bug.\n\nRené\n"}]}