mirror of
https://github.com/sudo-project/sudo.git
synced 2025-08-31 14:25:15 +00:00
Add ssizeof macro that returns ssize_t.
We can use this instead of casting the result of size_t to int. Also change checks for snprintf() returning <=0 to <0.
This commit is contained in:
@@ -144,6 +144,9 @@
|
||||
# define mtim_get(_x, _y) do { (_y).tv_sec = (_x)->st_mtime; (_y).tv_nsec = 0; } while (0)
|
||||
#endif /* HAVE_ST_MTIM */
|
||||
|
||||
/* sizeof() that returns a signed value */
|
||||
#define ssizeof(_x) ((ssize_t)sizeof(_x))
|
||||
|
||||
/* Bit map macros. */
|
||||
#define sudo_setbit(_a, _i) ((_a)[(_i) / NBBY] |= 1 << ((_i) % NBBY))
|
||||
#define sudo_clrbit(_a, _i) ((_a)[(_i) / NBBY] &= ~(1<<((_i) % NBBY)))
|
||||
|
@@ -208,7 +208,7 @@ warning(const char *errstr, const char *fmt, va_list ap)
|
||||
va_copy(ap2, ap);
|
||||
buflen = vsnprintf(static_buf, sizeof(static_buf), fmt, ap2);
|
||||
va_end(ap2);
|
||||
if (buflen >= (int)sizeof(static_buf)) {
|
||||
if (buflen >= ssizeof(static_buf)) {
|
||||
buf = malloc(++buflen);
|
||||
if (buf != NULL)
|
||||
(void)vsnprintf(buf, buflen, fmt, ap);
|
||||
|
@@ -79,7 +79,7 @@ inet_ntop4(const unsigned char *src, char *dst, socklen_t size)
|
||||
int len;
|
||||
|
||||
len = snprintf(dst, size, fmt, src[0], src[1], src[2], src[3]);
|
||||
if (len <= 0 || len >= size) {
|
||||
if (len < 0 || len >= size) {
|
||||
errno = ENOSPC;
|
||||
return (NULL);
|
||||
}
|
||||
|
@@ -99,7 +99,7 @@ main(int argc, char *argv[])
|
||||
/* Test small buffer w/ errno. */
|
||||
len = snprintf(buf1, sizeof(buf1),
|
||||
"unable to open %s: %s", "/var/log/sudo-io/seq", strerror(ENOENT));
|
||||
if (len < 0 || len >= (int)sizeof(buf1))
|
||||
if (len < 0 || len >= ssizeof(buf1))
|
||||
sudo_warnx_nodebug("buf1 trucated at %s:%d", __FILE__, __LINE__);
|
||||
expected_result = buf1;
|
||||
errno = ENOENT;
|
||||
@@ -115,7 +115,7 @@ main(int argc, char *argv[])
|
||||
memset(buf1, 'b', 8184);
|
||||
buf1[8184] = '\0';
|
||||
len = snprintf(buf2, sizeof(buf2), "%s: %s", buf1, strerror(EINVAL));
|
||||
if (len < 0 || len >= (int)sizeof(buf2))
|
||||
if (len < 0 || len >= ssizeof(buf2))
|
||||
sudo_warnx_nodebug("buf2 trucated at %s:%d", __FILE__, __LINE__);
|
||||
expected_result = buf2;
|
||||
errno = EINVAL;
|
||||
@@ -125,7 +125,7 @@ main(int argc, char *argv[])
|
||||
memset(buf1, 'b', 8184);
|
||||
buf1[8184] = '\0';
|
||||
len = snprintf(buf2, sizeof(buf2), "%.*s", 2047, buf1);
|
||||
if (len < 0 || len >= (int)sizeof(buf2))
|
||||
if (len < 0 || len >= ssizeof(buf2))
|
||||
sudo_warnx_nodebug("buf2 trucated at %s:%d", __FILE__, __LINE__);
|
||||
expected_result = buf2;
|
||||
test_vsyslog(0, buf1);
|
||||
|
@@ -653,7 +653,7 @@ sudo_debug_vprintf2_v1(const char *func, const char *file, int lineno, int level
|
||||
va_copy(ap2, ap);
|
||||
buflen = fmt ? vsnprintf(static_buf, sizeof(static_buf), fmt, ap2) : 0;
|
||||
va_end(ap2);
|
||||
if (buflen >= (int)sizeof(static_buf)) {
|
||||
if (buflen >= ssizeof(static_buf)) {
|
||||
va_list ap3;
|
||||
|
||||
/* Not enough room in static buf, allocate dynamically. */
|
||||
@@ -763,7 +763,7 @@ sudo_debug_execve2_v1(int level, const char *path, char *const argv[], char *con
|
||||
buflen += strlen(*av) + 1;
|
||||
buflen--;
|
||||
}
|
||||
if (buflen >= (int)sizeof(static_buf)) {
|
||||
if (buflen >= ssizeof(static_buf)) {
|
||||
buf = malloc(buflen + 1);
|
||||
if (buf == NULL)
|
||||
goto out;
|
||||
|
@@ -142,7 +142,7 @@ find_path(const char *infile, char **outfile, struct stat *sbp,
|
||||
*/
|
||||
len = snprintf(command, sizeof(command), "%.*s/%s",
|
||||
(int)(ep - cp), cp, infile);
|
||||
if (len <= 0 || len >= (int)sizeof(command)) {
|
||||
if (len < 0 || len >= ssizeof(command)) {
|
||||
errno = ENAMETOOLONG;
|
||||
debug_return_int(NOT_FOUND_ERROR);
|
||||
}
|
||||
@@ -156,7 +156,7 @@ find_path(const char *infile, char **outfile, struct stat *sbp,
|
||||
*/
|
||||
if (!found && checkdot) {
|
||||
len = snprintf(command, sizeof(command), "./%s", infile);
|
||||
if (len <= 0 || len >= (int)sizeof(command)) {
|
||||
if (len < 0 || len >= ssizeof(command)) {
|
||||
errno = ENAMETOOLONG;
|
||||
debug_return_int(NOT_FOUND_ERROR);
|
||||
}
|
||||
|
@@ -74,7 +74,7 @@ group_plugin_load(char *plugin_info)
|
||||
len = snprintf(path, sizeof(path), "%s%s",
|
||||
(*plugin_info != '/') ? path_plugin_dir : "", plugin_info);
|
||||
}
|
||||
if (len <= 0 || len >= (int)sizeof(path)) {
|
||||
if (len < 0 || len >= ssizeof(path)) {
|
||||
errno = ENAMETOOLONG;
|
||||
sudo_warn("%s%s",
|
||||
(*plugin_info != '/') ? path_plugin_dir : "", plugin_info);
|
||||
|
@@ -403,7 +403,7 @@ io_nextid(char *iolog_dir, char *iolog_dir_fallback, char sessid[7])
|
||||
* Open sequence file
|
||||
*/
|
||||
len = snprintf(pathbuf, sizeof(pathbuf), "%s/seq", iolog_dir);
|
||||
if (len <= 0 || len >= (int)sizeof(pathbuf)) {
|
||||
if (len < 0 || len >= ssizeof(pathbuf)) {
|
||||
errno = ENAMETOOLONG;
|
||||
log_warning(SLOG_SEND_MAIL, "%s/seq", pathbuf);
|
||||
goto done;
|
||||
@@ -431,7 +431,7 @@ io_nextid(char *iolog_dir, char *iolog_dir_fallback, char sessid[7])
|
||||
|
||||
len = snprintf(fallback, sizeof(fallback), "%s/seq",
|
||||
iolog_dir_fallback);
|
||||
if (len > 0 && len < (int)sizeof(fallback)) {
|
||||
if (len > 0 && len < ssizeof(fallback)) {
|
||||
int fd2 = io_open(fallback, O_RDWR|O_CREAT, iolog_filemode);
|
||||
if (fd2 != -1) {
|
||||
if (fchown(fd2, iolog_uid, iolog_gid) != 0) {
|
||||
|
@@ -492,7 +492,7 @@ sudo_ldap_timefilter(char *buffer, size_t buffersize)
|
||||
/* Build filter. */
|
||||
len = snprintf(buffer, buffersize, "(&(|(!(sudoNotAfter=*))(sudoNotAfter>=%s))(|(!(sudoNotBefore=*))(sudoNotBefore<=%s)))",
|
||||
timebuffer, timebuffer);
|
||||
if (len <= 0 || (size_t)len >= buffersize) {
|
||||
if (len < 0 || (size_t)len >= buffersize) {
|
||||
sudo_warnx(U_("internal error, %s overflow"), __func__);
|
||||
errno = EOVERFLOW;
|
||||
len = -1;
|
||||
|
@@ -201,7 +201,7 @@ sudo_ldap_conf_add_ports(void)
|
||||
|
||||
hostbuf[0] = '\0';
|
||||
len = snprintf(defport, sizeof(defport), ":%d", ldap_conf.port);
|
||||
if (len <= 0 || len >= (int)sizeof(defport)) {
|
||||
if (len < 0 || len >= ssizeof(defport)) {
|
||||
sudo_warnx(U_("sudo_ldap_conf_add_ports: port too large"));
|
||||
debug_return_bool(false);
|
||||
}
|
||||
|
@@ -466,7 +466,7 @@ fmt_authfail_message(char **str, va_list ap)
|
||||
break;
|
||||
case 'd':
|
||||
len = snprintf(dst, dst_end - dst, "%u", tries);
|
||||
if (len <= 0 || len >= (int)(dst_end - dst))
|
||||
if (len < 0 || len >= (int)(dst_end - dst))
|
||||
goto done;
|
||||
dst += len;
|
||||
src += 2;
|
||||
|
@@ -1246,7 +1246,7 @@ create_admin_success_flag(void)
|
||||
/* Build path to flag file. */
|
||||
len = snprintf(flagfile, sizeof(flagfile), "%s/.sudo_as_admin_successful",
|
||||
user_dir);
|
||||
if (len <= 0 || len >= (int)sizeof(flagfile))
|
||||
if (len < 0 || len >= ssizeof(flagfile))
|
||||
debug_return_int(false);
|
||||
|
||||
/* Create admin flag file if it doesn't already exist. */
|
||||
|
@@ -317,13 +317,13 @@ main(int argc, char *argv[])
|
||||
if (VALID_ID(id)) {
|
||||
plen = snprintf(path, sizeof(path), "%s/%.2s/%.2s/%.2s/timing",
|
||||
session_dir, id, &id[2], &id[4]);
|
||||
if (plen <= 0 || plen >= (int)sizeof(path))
|
||||
if (plen < 0 || plen >= ssizeof(path))
|
||||
sudo_fatalx(U_("%s/%.2s/%.2s/%.2s/timing: %s"), session_dir,
|
||||
id, &id[2], &id[4], strerror(ENAMETOOLONG));
|
||||
} else {
|
||||
plen = snprintf(path, sizeof(path), "%s/%s/timing",
|
||||
session_dir, id);
|
||||
if (plen <= 0 || plen >= (int)sizeof(path))
|
||||
if (plen < 0 || plen >= ssizeof(path))
|
||||
sudo_fatalx(U_("%s/%s/timing: %s"), session_dir,
|
||||
id, strerror(ENAMETOOLONG));
|
||||
}
|
||||
@@ -619,7 +619,7 @@ xterm_set_size(int rows, int cols)
|
||||
|
||||
/* XXX - save cursor and position restore after resizing */
|
||||
len = snprintf(buf, sizeof(buf), setsize_fmt, rows, cols);
|
||||
if (len <= 0 || len >= (int)sizeof(buf)) {
|
||||
if (len < 0 || len >= ssizeof(buf)) {
|
||||
/* not possible due to size of buf */
|
||||
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
|
||||
"%s: internal error, buffer too small?", __func__);
|
||||
@@ -1473,7 +1473,7 @@ find_sessions(const char *dir, regex_t *re, const char *user, const char *tty)
|
||||
for (i = 0; i < sessions_len; i++) {
|
||||
len = snprintf(&pathbuf[sdlen], sizeof(pathbuf) - sdlen,
|
||||
"%s/log", sessions[i]);
|
||||
if (len <= 0 || (size_t)len >= sizeof(pathbuf) - sdlen) {
|
||||
if (len < 0 || (size_t)len >= sizeof(pathbuf) - sdlen) {
|
||||
errno = ENAMETOOLONG;
|
||||
sudo_fatal("%s/%s/log", dir, sessions[i]);
|
||||
}
|
||||
|
@@ -1023,7 +1023,7 @@ already_lectured(int unused)
|
||||
if (ts_secure_dir(def_lecture_status_dir, false, true)) {
|
||||
len = snprintf(status_file, sizeof(status_file), "%s/%s",
|
||||
def_lecture_status_dir, user_name);
|
||||
if (len > 0 && len < (int)sizeof(status_file)) {
|
||||
if (len > 0 && len < ssizeof(status_file)) {
|
||||
debug_return_bool(stat(status_file, &sb) == 0);
|
||||
}
|
||||
log_warningx(SLOG_SEND_MAIL, N_("lecture status path too long: %s/%s"),
|
||||
@@ -1045,7 +1045,7 @@ set_lectured(void)
|
||||
|
||||
len = snprintf(lecture_status, sizeof(lecture_status), "%s/%s",
|
||||
def_lecture_status_dir, user_name);
|
||||
if (len <= 0 || len >= (int)sizeof(lecture_status)) {
|
||||
if (len < 0 || len >= ssizeof(lecture_status)) {
|
||||
log_warningx(SLOG_SEND_MAIL, N_("lecture status path too long: %s/%s"),
|
||||
def_lecture_status_dir, user_name);
|
||||
goto done;
|
||||
|
@@ -88,7 +88,7 @@ sudo_stat_plugin(struct plugin_info *info, char *fullpath,
|
||||
|
||||
len = snprintf(fullpath, pathsize, "%s%s", sudo_conf_plugin_dir_path(),
|
||||
info->path);
|
||||
if (len <= 0 || (size_t)len >= pathsize) {
|
||||
if (len < 0 || (size_t)len >= pathsize) {
|
||||
sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
|
||||
_PATH_SUDO_CONF, info->lineno, info->symbol_name);
|
||||
sudo_warnx(U_("%s%s: %s"), sudo_conf_plugin_dir_path(), info->path,
|
||||
|
@@ -179,7 +179,7 @@ get_net_ifs(char **addrinfo)
|
||||
|
||||
len = snprintf(cp, ailen - (*addrinfo - cp),
|
||||
"%s%s/%s", cp == *addrinfo ? "" : " ", addrstr, maskstr);
|
||||
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
|
||||
if (len < 0 || len >= ailen - (*addrinfo - cp)) {
|
||||
sudo_warnx(U_("internal error, %s overflow"), __func__);
|
||||
goto done;
|
||||
}
|
||||
@@ -196,7 +196,7 @@ get_net_ifs(char **addrinfo)
|
||||
|
||||
len = snprintf(cp, ailen - (*addrinfo - cp),
|
||||
"%s%s/%s", cp == *addrinfo ? "" : " ", addrstr, maskstr);
|
||||
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
|
||||
if (len < 0 || len >= ailen - (*addrinfo - cp)) {
|
||||
sudo_warnx(U_("internal error, %s overflow"), __func__);
|
||||
goto done;
|
||||
}
|
||||
@@ -341,7 +341,7 @@ get_net_ifs(char **addrinfo)
|
||||
|
||||
len = snprintf(cp, ailen - (*addrinfo - cp),
|
||||
"%s%s/%s", cp == *addrinfo ? "" : " ", addrstr, maskstr);
|
||||
if (len <= 0 || len >= ailen - (*addrinfo - cp)) {
|
||||
if (len < 0 || len >= ailen - (*addrinfo - cp)) {
|
||||
sudo_warnx(U_("internal error, %s overflow"), __func__);
|
||||
goto done;
|
||||
}
|
||||
|
@@ -468,7 +468,7 @@ get_user_groups(struct user_details *ud)
|
||||
for (i = 0; i < ud->ngroups; i++) {
|
||||
len = snprintf(cp, glsize - (cp - gid_list), "%s%u",
|
||||
i ? "," : "", (unsigned int)ud->groups[i]);
|
||||
if (len <= 0 || (size_t)len >= glsize - (cp - gid_list))
|
||||
if (len < 0 || (size_t)len >= glsize - (cp - gid_list))
|
||||
sudo_fatalx(U_("internal error, %s overflow"), __func__);
|
||||
cp += len;
|
||||
}
|
||||
@@ -864,7 +864,7 @@ sudo_check_suid(const char *sudo)
|
||||
|
||||
int len = snprintf(pathbuf, sizeof(pathbuf), "%.*s/%s",
|
||||
(int)(ep - cp), cp, sudo);
|
||||
if (len <= 0 || len >= (int)sizeof(pathbuf))
|
||||
if (len < 0 || len >= ssizeof(pathbuf))
|
||||
continue;
|
||||
if (access(pathbuf, X_OK) == 0) {
|
||||
sudo = pathbuf;
|
||||
|
Reference in New Issue
Block a user