summaryrefslogtreecommitdiff
path: root/src/errors.c
diff options
context:
space:
mode:
authorRussell Belfer <arrbee@arrbee.com>2012-03-13 14:23:24 -0700
committerRussell Belfer <arrbee@arrbee.com>2012-03-13 14:23:24 -0700
commite3c475107045cb89c53c114716bafebc7538433f (patch)
treeeb6df56dc41633a30a935058477e45b8e39ef77a /src/errors.c
parent1736799d2a15d912cfc46b7089c2bff02a1cbd0e (diff)
downloadlibgit2-e3c475107045cb89c53c114716bafebc7538433f.tar.gz
Resolve comments from pull request
This converts the map validation function into a macro, tweaks the GITERR_OS system error automatic appending, and adds a tentative new error access API and some quick unit tests for both the old and new error APIs.
Diffstat (limited to 'src/errors.c')
-rw-r--r--src/errors.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/errors.c b/src/errors.c
index 19bc7b77b..70aa641c4 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -123,33 +123,41 @@ void giterr_set(int error_class, const char *string, ...)
char error_str[1024];
va_list arglist;
+ /* Grab errno before calling vsnprintf() so it won't be overwritten */
+ const char *os_error_msg =
+ (error_class == GITERR_OS && errno != 0) ? strerror(errno) : NULL;
+#ifdef GIT_WIN32
+ DWORD dwLastError = GetLastError();
+#endif
+
va_start(arglist, string);
p_vsnprintf(error_str, sizeof(error_str), string, arglist);
va_end(arglist);
/* automatically suffix strerror(errno) for GITERR_OS errors */
if (error_class == GITERR_OS) {
- if (errno != 0) {
+ if (os_error_msg != NULL) {
strncat(error_str, ": ", sizeof(error_str));
- strncat(error_str, strerror(errno), sizeof(error_str));
- errno = 0;
+ strncat(error_str, os_error_msg, sizeof(error_str));
+ errno = 0; /* reset so same error won't be reported twice */
}
#ifdef GIT_WIN32
- else {
- LPVOID lpMsgBuf;
- DWORD dw = GetLastError();
+ else if (dwLastError != 0) {
+ LPVOID lpMsgBuf = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, dw, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
+ NULL, dwLastError, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
if (lpMsgBuf) {
strncat(error_str, ": ", sizeof(error_str));
strncat(error_str, (const char *)lpMsgBuf, sizeof(error_str));
LocalFree(lpMsgBuf);
}
+
+ SetLastError(0);
}
#endif
}
@@ -185,3 +193,14 @@ void giterr_clear(void)
{
GIT_GLOBAL->last_error = NULL;
}
+
+const git_error *git_error_last(void)
+{
+ return GIT_GLOBAL->last_error;
+}
+
+void git_error_clear(void)
+{
+ giterr_clear();
+}
+