2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-09-03 15:55:40 +00:00

Wrap error/errorx and warning/warningx functions with debug statements.

Disable wrapping for standalone sudoers programs as well as memory
allocation functions (to avoid infinite recursion).
This commit is contained in:
Todd C. Miller
2011-10-22 14:28:33 -04:00
parent 749a7695d0
commit 9923464d96
7 changed files with 84 additions and 24 deletions

View File

@@ -79,10 +79,10 @@ emalloc(size_t size)
void *ptr;
if (size == 0)
errorx(1, _("internal error, tried to emalloc(0)"));
errorx2(1, _("internal error, tried to emalloc(0)"));
if ((ptr = malloc(size)) == NULL)
errorx(1, _("unable to allocate memory"));
errorx2(1, _("unable to allocate memory"));
return ptr;
}
@@ -96,13 +96,13 @@ emalloc2(size_t nmemb, size_t size)
void *ptr;
if (nmemb == 0 || size == 0)
errorx(1, _("internal error, tried to emalloc2(0)"));
errorx2(1, _("internal error, tried to emalloc2(0)"));
if (nmemb > SIZE_MAX / size)
errorx(1, _("internal error, emalloc2() overflow"));
errorx2(1, _("internal error, emalloc2() overflow"));
size *= nmemb;
if ((ptr = malloc(size)) == NULL)
errorx(1, _("unable to allocate memory"));
errorx2(1, _("unable to allocate memory"));
return ptr;
}
@@ -116,11 +116,11 @@ erealloc(void *ptr, size_t size)
{
if (size == 0)
errorx(1, _("internal error, tried to erealloc(0)"));
errorx2(1, _("internal error, tried to erealloc(0)"));
ptr = ptr ? realloc(ptr, size) : malloc(size);
if (ptr == NULL)
errorx(1, _("unable to allocate memory"));
errorx2(1, _("unable to allocate memory"));
return ptr;
}
@@ -135,14 +135,14 @@ erealloc3(void *ptr, size_t nmemb, size_t size)
{
if (nmemb == 0 || size == 0)
errorx(1, _("internal error, tried to erealloc3(0)"));
errorx2(1, _("internal error, tried to erealloc3(0)"));
if (nmemb > SIZE_MAX / size)
errorx(1, _("internal error, erealloc3() overflow"));
errorx2(1, _("internal error, erealloc3() overflow"));
size *= nmemb;
ptr = ptr ? realloc(ptr, size) : malloc(size);
if (ptr == NULL)
errorx(1, _("unable to allocate memory"));
errorx2(1, _("unable to allocate memory"));
return ptr;
}
@@ -200,7 +200,7 @@ easprintf(char **ret, const char *fmt, ...)
va_end(ap);
if (len == -1)
errorx(1, _("unable to allocate memory"));
errorx2(1, _("unable to allocate memory"));
return len;
}
@@ -214,7 +214,7 @@ evasprintf(char **ret, const char *format, va_list args)
int len;
if ((len = vasprintf(ret, format, args)) == -1)
errorx(1, _("unable to allocate memory"));
errorx2(1, _("unable to allocate memory"));
return len;
}

View File

@@ -19,9 +19,63 @@
#include <stdarg.h>
void error(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__));
void errorx(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__));
void warning(const char *, ...) __printflike(1, 2);
void warningx(const char *, ...) __printflike(1, 2);
/*
* We wrap error/errorx and warn/warnx so that the same output can
* go to the debug file, if there is one.
*/
#if defined(SUDO_ERROR_WRAP) && SUDO_ERROR_WRAP == 0
# if defined(__GNUC__) && __GNUC__ == 2
# define error(rval, fmt...) error2((rval), (fmt))
# define errorx(rval, fmt...) errorx2((rval), (fmt))
# define warning(fmt...) warning2((fmt))
# define warningx(fmt...) warningx2((fmt))
# else
# define error(rval, ...) error2((rval), __VA_ARGS__)
# define errorx(rval, ...) errorx2((rval), __VA_ARGS__)
# define warning(...) warning2(__VA_ARGS__)
# define warningx(...) warningx2(__VA_ARGS__)
# endif /* __GNUC__ == 2 */
#else /* SUDO_ERROR_WRAP */
# if defined(__GNUC__) && __GNUC__ == 2
# define error(rval, fmt...) do { \
sudo_debug_printf2(SUDO_DEBUG_SYSERR|sudo_debug_subsys, (fmt)); \
error2((rval), (fmt)); \
} while (0)
# define errorx(rval, fmt...) do { \
sudo_debug_printf2(SUDO_DEBUG_PROGERR|sudo_debug_subsys, (fmt)); \
errorx2((rval), (fmt)); \
} while (0)
# define warning(fmt...) do { \
sudo_debug_printf2(SUDO_DEBUG_SYSERR|sudo_debug_subsys, (fmt)); \
warning2((fmt)); \
} while (0)
# define warningx(fmt...) do { \
sudo_debug_printf2(SUDO_DEBUG_PROGERR|sudo_debug_subsys, (fmt)); \
warningx2((fmt)); \
} while (0)
# else
# define error(rval, ...) do { \
sudo_debug_printf2(SUDO_DEBUG_SYSERR|sudo_debug_subsys, __VA_ARGS__); \
error2((rval), __VA_ARGS__); \
} while (0)
# define errorx(rval, ...) do { \
sudo_debug_printf2(SUDO_DEBUG_PROGERR|sudo_debug_subsys, __VA_ARGS__); \
errorx2((rval), __VA_ARGS__); \
} while (0)
# define warning(...) do { \
sudo_debug_printf2(SUDO_DEBUG_SYSERR|sudo_debug_subsys, __VA_ARGS__); \
warning2(__VA_ARGS__); \
} while (0)
# define warningx(...) do { \
sudo_debug_printf2(SUDO_DEBUG_PROGERR|sudo_debug_subsys, __VA_ARGS__); \
warningx2(__VA_ARGS__); \
} while (0)
# endif /* __GNUC__ == 2 */
#endif /* SUDO_ERROR_WRAP */
void error2(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__));
void errorx2(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__));
void warning2(const char *, ...) __printflike(1, 2);
void warningx2(const char *, ...) __printflike(1, 2);
#endif /* _SUDO_ERROR_H_ */

View File

@@ -40,7 +40,7 @@ sigjmp_buf error_jmp;
extern sudo_conv_t sudo_conv;
void
error(int eval, const char *fmt, ...)
error2(int eval, const char *fmt, ...)
{
va_list ap;
@@ -52,7 +52,7 @@ error(int eval, const char *fmt, ...)
}
void
errorx(int eval, const char *fmt, ...)
errorx2(int eval, const char *fmt, ...)
{
va_list ap;
@@ -64,7 +64,7 @@ errorx(int eval, const char *fmt, ...)
}
void
warning(const char *fmt, ...)
warning2(const char *fmt, ...)
{
va_list ap;
@@ -74,7 +74,7 @@ warning(const char *fmt, ...)
}
void
warningx(const char *fmt, ...)
warningx2(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);

View File

@@ -88,6 +88,8 @@
#include <pathnames.h>
#define SUDO_ERROR_WRAP 0 /* XXX */
#include "missing.h"
#include "alloc.h"
#include "error.h"

View File

@@ -59,6 +59,8 @@
#include <arpa/inet.h>
#include <netdb.h>
#define SUDO_ERROR_WRAP 0 /* XXX */
#include "tsgetgrpw.h"
#include "sudoers.h"
#include "interfaces.h"

View File

@@ -75,6 +75,8 @@
# include <locale.h>
#endif
#define SUDO_ERROR_WRAP 0 /* XXX */
#include "sudoers.h"
#include "interfaces.h"
#include "parse.h"

View File

@@ -33,7 +33,7 @@ static void _warning(int, const char *, va_list);
void cleanup(int);
void
error(int eval, const char *fmt, ...)
error2(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
@@ -44,7 +44,7 @@ error(int eval, const char *fmt, ...)
}
void
errorx(int eval, const char *fmt, ...)
errorx2(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
@@ -55,7 +55,7 @@ errorx(int eval, const char *fmt, ...)
}
void
warning(const char *fmt, ...)
warning2(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
@@ -64,7 +64,7 @@ warning(const char *fmt, ...)
}
void
warningx(const char *fmt, ...)
warningx2(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);