Avoid strerror() when possible and just rely on warning/error

to handle errno in the proper locale.
This commit is contained in:
Todd C. Miller
2012-11-09 16:32:29 -05:00
parent e28ce01fe0
commit 56de023de8
6 changed files with 27 additions and 16 deletions

View File

@@ -65,8 +65,10 @@ find_path(char *infile, char **outfile, struct stat *sbp, char *path,
int len; /* length parameter */
debug_decl(find_path, SUDO_DEBUG_UTIL)
if (strlen(infile) >= PATH_MAX)
errorx(1, _("%s: %s"), infile, strerror(ENAMETOOLONG));
if (strlen(infile) >= PATH_MAX) {
errno = ENAMETOOLONG;
error(1, "%s", infile);
}
/*
* If we were given a fully qualified or relative path
@@ -104,8 +106,10 @@ find_path(char *infile, char **outfile, struct stat *sbp, char *path,
* Resolve the path and exit the loop if found.
*/
len = snprintf(command, sizeof(command), "%s/%s", path, infile);
if (len <= 0 || len >= sizeof(command))
errorx(1, _("%s: %s"), infile, strerror(ENAMETOOLONG));
if (len <= 0 || len >= sizeof(command)) {
errno = ENAMETOOLONG;
error(1, "%s", infile);
}
if ((found = sudo_goodpath(command, sbp)))
break;
@@ -119,8 +123,10 @@ find_path(char *infile, char **outfile, struct stat *sbp, char *path,
*/
if (!found && checkdot) {
len = snprintf(command, sizeof(command), "./%s", infile);
if (len <= 0 || len >= sizeof(command))
errorx(1, _("%s: %s"), infile, strerror(ENAMETOOLONG));
if (len <= 0 || len >= sizeof(command)) {
errno = ENAMETOOLONG;
error(1, "%s", infile);
}
found = sudo_goodpath(command, sbp);
if (found && ignore_dot)
debug_return_int(NOT_FOUND_DOT);

View File

@@ -88,9 +88,9 @@ group_plugin_load(char *plugin_info)
(*plugin_info != '/') ? _PATH_SUDO_PLUGIN_DIR : "", plugin_info);
}
if (len <= 0 || len >= sizeof(path)) {
warningx(N_("%s%s: %s"),
(*plugin_info != '/') ? _PATH_SUDO_PLUGIN_DIR : "", plugin_info,
strerror(ENAMETOOLONG));
errno = ENAMETOOLONG;
warning("%s%s",
(*plugin_info != '/') ? _PATH_SUDO_PLUGIN_DIR : "", plugin_info);
goto done;
}

View File

@@ -121,7 +121,8 @@ mkdir_parents(char *path)
if (mkdir(path, S_IRWXU) != 0)
log_fatal(USE_ERRNO, N_("unable to mkdir %s"), path);
} else if (!S_ISDIR(sb.st_mode)) {
log_fatal(0, N_("%s: %s"), path, strerror(ENOTDIR));
errno = ENOTDIR;
log_fatal(USE_ERRNO, "%s", path);
}
*slash = '/';
}

View File

@@ -40,6 +40,7 @@
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <setjmp.h>
@@ -493,8 +494,10 @@ sudoers_policy_close(int exit_status, int error_code)
}
/* We do not currently log the exit status. */
if (error_code)
warningx(N_("unable to execute %s: %s"), safe_cmnd, strerror(error_code));
if (error_code) {
errno = error_code;
warning(N_("unable to execute %s"), safe_cmnd);
}
/* Close the session we opened in sudoers_policy_init_session(). */
if (ISSET(sudo_mode, MODE_RUN|MODE_EDIT))

View File

@@ -685,8 +685,10 @@ set_cmnd(void)
}
}
}
if (strlen(user_cmnd) >= PATH_MAX)
errorx(1, _("%s: %s"), user_cmnd, strerror(ENAMETOOLONG));
if (strlen(user_cmnd) >= PATH_MAX) {
errno = ENAMETOOLONG;
error(1, "%s", user_cmnd);
}
if ((user_base = strrchr(user_cmnd, '/')) != NULL)
user_base++;

View File

@@ -400,8 +400,7 @@ remove_timestamp(bool remove)
status = rmdir(timestampdir);
if (status == -1 && errno != ENOENT) {
log_error(0,
N_("unable to remove %s (%s), will reset to the epoch"),
path, strerror(errno));
N_("unable to remove %s, will reset to the epoch"), path);
remove = false;
}
}