{"thread":{"id":"26946","subject":"[PATCH] system_path: use a static buffer","startedAt":"2011-03-31T14:36:27Z","lastAt":"2011-03-31T23:23:34Z","messageCount":3,"participants":["Carlos Martín Nieto","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"164797","messageId":"1301582187-20340-1-git-send-email-cmn@elego.de","threadId":"26946","inReplyTo":null,"subject":"[PATCH] system_path: use a static buffer","fromName":"Carlos Martín Nieto","fromEmail":"cmn@elego.de","sentAt":"2011-03-31T14:36:27Z","receivedAt":"2011-03-31T14:36:27Z","isPatch":true,"sender":{"key":"cmn@elego.de","avatar":"https://avatars.githubusercontent.com/u/335443?v=4"},"body":"Make system_path behave like the other path functions by using a\nstatic buffer, plugging a memory leak.\n\nAlso make sure the prefix pointer is always initialized to either\nPREFIX or NULL.\n\ngit_etc_gitattributes and git_etc_gitconfig are the only users who are\naffected by this change. Make them use a static buffer, which fits\ntheir use better as well.\n\nSigned-off-by: Carlos Martín Nieto <cmn@elego.de>\n---\n\n Slightly changed version of the patch in pu with the error strings\nchanged. Junio, do you want this patch at all. I think in your \"what's\ncooking\" messages, you said you don't like this type of band-aid? As\nit's not that big a deal, I'll drop it if you don't want it.\n\n attr.c     |    6 +++---\n config.c   |    6 +++---\n exec_cmd.c |   15 ++++++++++-----\n 3 files changed, 16 insertions(+), 11 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 6aff695..64d803f 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -467,9 +467,9 @@ static void drop_attr_stack(void)\n \n const char *git_etc_gitattributes(void)\n {\n-\tstatic const char *system_wide;\n-\tif (!system_wide)\n-\t\tsystem_wide = system_path(ETC_GITATTRIBUTES);\n+\tstatic char system_wide[PATH_MAX];\n+\tif (!system_wide[0])\n+\t\tstrlcpy(system_wide, system_path(ETC_GITATTRIBUTES), PATH_MAX);\n \treturn system_wide;\n }\n \ndiff --git a/config.c b/config.c\nindex 822ef83..cd1c295 100644\n--- a/config.c\n+++ b/config.c\n@@ -808,9 +808,9 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)\n \n const char *git_etc_gitconfig(void)\n {\n-\tstatic const char *system_wide;\n-\tif (!system_wide)\n-\t\tsystem_wide = system_path(ETC_GITCONFIG);\n+\tstatic char system_wide[PATH_MAX];\n+\tif (!system_wide[0])\n+\t\tstrlcpy(system_wide, system_path(ETC_GITCONFIG), PATH_MAX);\n \treturn system_wide;\n }\n \ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex 38545e8..8d0fa49 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -9,11 +9,12 @@ static const char *argv0_path;\n const char *system_path(const char *path)\n {\n #ifdef RUNTIME_PREFIX\n-\tstatic const char *prefix;\n+\tstatic const char *prefix = NULL;\n #else\n \tstatic const char *prefix = PREFIX;\n #endif\n-\tstruct strbuf d = STRBUF_INIT;\n+\tstatic char buf[PATH_MAX];\n+\tint ret;\n \n \tif (is_absolute_path(path))\n \t\treturn path;\n@@ -33,9 +34,13 @@ const char *system_path(const char *path)\n \t}\n #endif\n \n-\tstrbuf_addf(&d, \"%s/%s\", prefix, path);\n-\tpath = strbuf_detach(&d, NULL);\n-\treturn path;\n+\tret = snprintf(buf, sizeof(buf), \"%s/%s\", prefix, path);\n+\tif (ret >= sizeof(buf))\n+\t\tdie(\"system path too long\");\n+\telse if (ret < 0)\n+\t\tdie_errno(\"snprintf reported an error\");\n+\n+\treturn buf;\n }\n \n const char *git_extract_argv0_path(const char *argv0)\n-- \n1.7.4.1\n"},{"id":"164842","messageId":"7voc4qhpyk.fsf@alter.siamese.dyndns.org","threadId":"26946","inReplyTo":"1301582187-20340-1-git-send-email-cmn@elego.de","subject":"Re: [PATCH] system_path: use a static buffer","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-03-31T22:42:27Z","receivedAt":"2011-03-31T22:42:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Carlos Martín Nieto <cmn@elego.de> writes:\n\n>  Slightly changed version of the patch in pu with the error strings\n> changed. Junio, do you want this patch at all. I think in your \"what's\n> cooking\" messages, you said you don't like this type of band-aid? As\n> it's not that big a deal, I'll drop it if you don't want it.\n\nThe \"band-aid\" refers to the topic listed after your topic, I think.  I\ndidn't even have a comment on your topic in the message.\n\nWe'll be in pre-release freeze for 1.7.5 soon, and I want to see patches\nthat deal with internal clean-up held for now and resubmit after 1.7.5 is\nreleased.  This cycle lasted a bit too long but hopefully we can conclude\nit by the mid April.\n\nThanks.\n"},{"id":"164846","messageId":"1301613826.26974.11.camel@centaur","threadId":"26946","inReplyTo":"7voc4qhpyk.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH] system_path: use a static buffer","fromName":"Carlos Martín Nieto","fromEmail":"cmn@elego.de","sentAt":"2011-03-31T23:23:34Z","receivedAt":"2011-03-31T23:23:34Z","isPatch":true,"sender":{"key":"cmn@elego.de","avatar":"https://avatars.githubusercontent.com/u/335443?v=4"},"body":"On Thu, 2011-03-31 at 15:42 -0700, Junio C Hamano wrote:\n> Carlos Martín Nieto <cmn@elego.de> writes:\n> \n> >  Slightly changed version of the patch in pu with the error strings\n> > changed. Junio, do you want this patch at all. I think in your \"what's\n> > cooking\" messages, you said you don't like this type of band-aid? As\n> > it's not that big a deal, I'll drop it if you don't want it.\n> \n> The \"band-aid\" refers to the topic listed after your topic, I think.  I\n> didn't even have a comment on your topic in the message.\n\n OK, I misunderstood.\n\n> \n> We'll be in pre-release freeze for 1.7.5 soon, and I want to see patches\n> that deal with internal clean-up held for now and resubmit after 1.7.5 is\n> released.  This cycle lasted a bit too long but hopefully we can conclude\n> it by the mid April.\n\n Fair enough. I'll re-send after the release.\n\n   cmn\n"}]}