-
Notifications
You must be signed in to change notification settings - Fork 142
vreportf: Fix interleaving issues #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,40 @@ | |
#include "git-compat-util.h" | ||
#include "cache.h" | ||
|
||
static void replace_control_chars(char *str, size_t size) | ||
{ | ||
size_t i; | ||
|
||
for (i = 0; i < size; i++) { | ||
if (iscntrl(str[i]) && str[i] != '\t' && str[i] != '\n') | ||
str[i] = '?'; | ||
} | ||
} | ||
|
||
/* Atomically report (prefix + vsnprintf(err, params) + '\n') to stderr. */ | ||
void vreportf(const char *prefix, const char *err, va_list params) | ||
{ | ||
char msg[4096]; | ||
char *p; | ||
int ret; | ||
size_t prefix_size, total_size; | ||
|
||
vsnprintf(msg, sizeof(msg), err, params); | ||
for (p = msg; *p; p++) { | ||
if (iscntrl(*p) && *p != '\t' && *p != '\n') | ||
*p = '?'; | ||
} | ||
fprintf(stderr, "%s%s\n", prefix, msg); | ||
prefix_size = strlen(prefix); | ||
if (prefix_size >= sizeof(msg)) | ||
BUG("vreportf: prefix is too long"); | ||
|
||
memcpy(msg, prefix, prefix_size); | ||
|
||
ret = vsnprintf(msg + prefix_size, sizeof(msg) - prefix_size, err, params); | ||
if (ret < 0) | ||
BUG("your vsnprintf is broken (returned %d)", ret); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'll cause an infinite loop, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to my knowledge, because BUG() uses a different format string, which isn't broken. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you agree? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion, this is too fragile. If we're here, things are already going wrong, and I'd rather not add a recursion into the same function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just exit silently? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say, if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In any case, Git requires a And I would certainly not introduce such a check in a commit that does many other things at the same time, too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will change that to exiting silently. |
||
|
||
total_size = strlen(msg); /* vsnprintf() returns _desired_ size */ | ||
msg[total_size++] = '\n'; /* it's ok to overwrite terminating NULL */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://linux.die.net/man/3/fprintf
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied from there, just used capital letters by habit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I don't think you want to hear what I have to say, so I'll just stop. |
||
|
||
replace_control_chars(msg, total_size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I find it totally unnecessary to refactor this part of the code into its own function. It's not like we introduce another caller, or that those four lines were particularly complex. In any case, a refactoring needs to go into its own commit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looked like a good function candidate to me. I guess git coding style also suggests to give names instead of writing comments. As for commit granularity, I'm not used to it. OK, I will inline the function again. |
||
|
||
fflush(stderr); /* flush FILE* before writing lowlevel fd */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment just repeats what the statement says, and is therefore non-DRY. A more interesting bit of information is why this is necessary, and this information naturally should go into the commit message, not into the code, as code comments like ones talking about specifics of a certain platform are prone to become stale, which we try to avoid in Git's source code. The reasoning behind a patch is pretty much tied to the commit itself, and should therefore be described in the commit message. Speaking of commit messages: the style you chose deviates noticeably from the style found in the existing commit history, and I wonder what reasons there are for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you suggest a better wording? It's necessary because FILE* and fd are different layers of abstraction. I think that problems can occur on any platform. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for commit message, what do you mean by style? Headers? I thought there's too much information to put in one stream, easier to navigate with headers. Just like in any other text. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I cannot suggest a better wording, as my suggestion to remove the unnecessary comment still stands. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't expect that an average reader will understand quickly why fflush() could be needed here, so sounds like exactly when comments are good? How about this wording? |
||
xwrite(2, msg, total_size); /* writing directly to fd is most atomic */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having seen this function, I see this PR edging closer and closer, step by step, to what I proposed already. There are still a couple of unnecessary bits and pieces that could be chopped off to arrive there, but I guess I really don't see what big improvements this PR offers compared to my proposed code (which was very similar to the code Jeff King proposed originally). But maybe I am still missing something crucial? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the absence of bugs counts much more then small personal preferences about this and that. |
||
} | ||
|
||
static NORETURN void usage_builtin(const char *err, va_list params) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Johannes Schindelin wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Alexandr Miloslavskiy wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Johannes Schindelin wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Alexandr Miloslavskiy wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Johannes Schindelin wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Johannes Schindelin wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Jeff King wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Johannes Schindelin wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Johannes Schindelin wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Johannes Schindelin wrote (reply to this):