s4:heimdal: import lorikeet-heimdal-202201172009 (commit 5a0b45cd723628b3690ea848548b...
[samba.git] / source4 / heimdal / lib / krb5 / warn.c
index cb3be76fccff5a2118a2be034a17feb112f341b8..65a7db6b8ce15c2ddfb7bf36651c379fdfd2fb8f 100644 (file)
  * SUCH DAMAGE.
  */
 
+#if defined(_MSC_VER)
+# pragma warning(disable: 4646)
+# pragma warning(disable: 4716)
+#endif
+
 #include "krb5_locl.h"
 #include <err.h>
 
 static krb5_error_code _warnerr(krb5_context context, int do_errtext,
         krb5_error_code code, int level, const char *fmt, va_list ap)
-       __attribute__((__format__(__printf__, 5, 0)));
+       __attribute__ ((__format__ (__printf__, 5, 0)));
 
 static krb5_error_code
 _warnerr(krb5_context context, int do_errtext,
         krb5_error_code code, int level, const char *fmt, va_list ap)
 {
-    char xfmt[7] = "";
-    const char *args[2], **arg;
-    char *msg = NULL;
-    const char *err_str = NULL;
-    krb5_error_code ret;
-
-    args[0] = args[1] = NULL;
-    arg = args;
-    if(fmt){
-       strlcat(xfmt, "%s", sizeof(xfmt));
-       if(do_errtext)
-           strlcat(xfmt, ": ", sizeof(xfmt));
-       ret = vasprintf(&msg, fmt, ap);
-       if(ret < 0 || msg == NULL)
-           return ENOMEM;
-       *arg++ = msg;
-    }
-    if(context && do_errtext){
-       strlcat(xfmt, "%s", sizeof(xfmt));
-
-       err_str = krb5_get_error_message(context, code);
-       if (err_str != NULL) {
-           *arg = err_str;
-       } else {
-           *arg= "<unknown error>";
-       }
-    }
-
-    if(context && context->warn_dest)
-       krb5_log(context, context->warn_dest, level, xfmt, args[0], args[1]);
+    if (do_errtext)
+        return heim_vwarn(context ? context->hcontext : NULL, code, fmt, ap);
     else
-       warnx(xfmt, args[0], args[1]);
-    free(msg);
-    krb5_free_error_message(context, err_str);
-    return 0;
+        return heim_vwarnx(context ? context->hcontext : NULL, fmt, ap);
 }
 
-#define FUNC(ETEXT, CODE, LEVEL)                                       \
-    krb5_error_code ret;                                               \
-    va_list ap;                                                                \
-    va_start(ap, fmt);                                                 \
-    ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap);              \
+#define FUNC_NORET(ETEXT, CODE, LEVEL)                                 \
+    va_list ap;                                                        \
+    va_start(ap, fmt);                                                 \
+    (void) _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap);             \
     va_end(ap);
 
 #undef __attribute__
@@ -104,9 +77,9 @@ _warnerr(krb5_context context, int do_errtext,
 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_vwarn(krb5_context context, krb5_error_code code,
           const char *fmt, va_list ap)
-     __attribute__ ((format (printf, 3, 0)))
+     __attribute__ ((__format__ (__printf__, 3, 0)))
 {
-    return _warnerr(context, 1, code, 1, fmt, ap);
+    return heim_vwarn(context ? context->hcontext : NULL, code, fmt, ap);
 }
 
 /**
@@ -122,9 +95,14 @@ krb5_vwarn(krb5_context context, krb5_error_code code,
 
 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...)
-     __attribute__ ((format (printf, 3, 4)))
+     __attribute__ ((__format__ (__printf__, 3, 4)))
 {
-    FUNC(1, code, 1);
+    krb5_error_code ret;
+    va_list ap;
+
+    va_start(ap, fmt);
+    ret = krb5_vwarn(context, code, fmt, ap);
+    va_end(ap);
     return ret;
 }
 
@@ -140,9 +118,9 @@ krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...)
 
 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_vwarnx(krb5_context context, const char *fmt, va_list ap)
-     __attribute__ ((format (printf, 2, 0)))
+     __attribute__ ((__format__ (__printf__, 2, 0)))
 {
-    return _warnerr(context, 0, 0, 1, fmt, ap);
+    return heim_vwarnx(context ? context->hcontext : NULL, fmt, ap);
 }
 
 /**
@@ -156,9 +134,14 @@ krb5_vwarnx(krb5_context context, const char *fmt, va_list ap)
 
 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_warnx(krb5_context context, const char *fmt, ...)
-     __attribute__ ((format (printf, 2, 3)))
+     __attribute__ ((__format__ (__printf__, 2, 3)))
 {
-    FUNC(0, 0, 1);
+    krb5_error_code ret;
+    va_list ap;
+
+    va_start(ap, fmt);
+    ret = krb5_vwarnx(context, fmt, ap);
+    va_end(ap);
     return ret;
 }
 
@@ -175,10 +158,10 @@ krb5_warnx(krb5_context context, const char *fmt, ...)
  * @ingroup krb5_error
  */
 
-KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
+KRB5_LIB_NORETURN_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_verr(krb5_context context, int eval, krb5_error_code code,
          const char *fmt, va_list ap)
-     __attribute__ ((noreturn, format (printf, 4, 0)))
+     __attribute__ ((__noreturn__, __format__ (__printf__, 4, 0)))
 {
     _warnerr(context, 1, code, 0, fmt, ap);
     exit(eval);
@@ -197,12 +180,12 @@ krb5_verr(krb5_context context, int eval, krb5_error_code code,
  * @ingroup krb5_error
  */
 
-KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
+KRB5_LIB_NORETURN_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_err(krb5_context context, int eval, krb5_error_code code,
         const char *fmt, ...)
-     __attribute__ ((noreturn, format (printf, 4, 5)))
+     __attribute__ ((__noreturn__, __format__ (__printf__, 4, 5)))
 {
-    FUNC(1, code, 0);
+    FUNC_NORET(1, code, 0);
     exit(eval);
     UNREACHABLE(return 0);
 }
@@ -218,9 +201,9 @@ krb5_err(krb5_context context, int eval, krb5_error_code code,
  * @ingroup krb5_error
  */
 
-KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
+KRB5_LIB_NORETURN_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap)
-     __attribute__ ((noreturn, format (printf, 3, 0)))
+     __attribute__ ((__noreturn__, __format__ (__printf__, 3, 0)))
 {
     _warnerr(context, 0, 0, 0, fmt, ap);
     exit(eval);
@@ -237,11 +220,11 @@ krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap)
  * @ingroup krb5_error
  */
 
-KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
+KRB5_LIB_NORETURN_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_errx(krb5_context context, int eval, const char *fmt, ...)
-     __attribute__ ((noreturn, format (printf, 3, 4)))
+     __attribute__ ((__noreturn__, __format__ (__printf__, 3, 4)))
 {
-    FUNC(0, 0, 0);
+    FUNC_NORET(0, 0, 0);
     exit(eval);
     UNREACHABLE(return 0);
 }
@@ -258,10 +241,10 @@ krb5_errx(krb5_context context, int eval, const char *fmt, ...)
  * @ingroup krb5_error
  */
 
-KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
+KRB5_LIB_NORETURN_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_vabort(krb5_context context, krb5_error_code code,
            const char *fmt, va_list ap)
-     __attribute__ ((noreturn, format (printf, 3, 0)))
+     __attribute__ ((__noreturn__, __format__ (__printf__, 3, 0)))
 {
     _warnerr(context, 1, code, 0, fmt, ap);
     abort();
@@ -275,22 +258,23 @@ krb5_vabort(krb5_context context, krb5_error_code code,
  * @param context A Kerberos 5 context
  * @param code error code of the last error
  * @param fmt message to print
+ * @param ... arguments for format string
  *
  * @ingroup krb5_error
  */
 
-KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
+KRB5_LIB_NORETURN_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_abort(krb5_context context, krb5_error_code code, const char *fmt, ...)
-     __attribute__ ((noreturn, format (printf, 3, 4)))
+     __attribute__ ((__noreturn__, __format__ (__printf__, 3, 4)))
 {
-    FUNC(1, code, 0);
+    FUNC_NORET(1, code, 0);
     abort();
     UNREACHABLE(return 0);
 }
 
-KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
+KRB5_LIB_NORETURN_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_vabortx(krb5_context context, const char *fmt, va_list ap)
-     __attribute__ ((noreturn, format (printf, 2, 0)))
+     __attribute__ ((__noreturn__, __format__ (__printf__, 2, 0)))
 {
     _warnerr(context, 0, 0, 0, fmt, ap);
     abort();
@@ -301,17 +285,17 @@ krb5_vabortx(krb5_context context, const char *fmt, va_list ap)
  * Log a warning to the log, default stderr, and then abort.
  *
  * @param context A Kerberos 5 context
- * @param code error code of the last error
- * @param fmt message to print
+ * @param fmt printf format string of message to print
+ * @param ... arguments for format string
  *
  * @ingroup krb5_error
  */
 
-KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
+KRB5_LIB_NORETURN_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_abortx(krb5_context context, const char *fmt, ...)
-     __attribute__ ((noreturn, format (printf, 2, 3)))
+     __attribute__ ((__noreturn__, __format__ (__printf__, 2, 3)))
 {
-    FUNC(0, 0, 0);
+    FUNC_NORET(0, 0, 0);
     abort();
     UNREACHABLE(return 0);
 }
@@ -328,8 +312,7 @@ krb5_abortx(krb5_context context, const char *fmt, ...)
 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
 krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
 {
-    context->warn_dest = fac;
-    return 0;
+    return heim_set_warn_dest(context->hcontext, fac);
 }
 
 /**
@@ -343,5 +326,5 @@ krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
 KRB5_LIB_FUNCTION krb5_log_facility * KRB5_LIB_CALL
 krb5_get_warn_dest(krb5_context context)
 {
-    return context->warn_dest;
+    return heim_get_warn_dest(context->hcontext);
 }