LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/krb5 - error_string.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 27 44 61.4 %
Date: 2024-02-29 22:57:05 Functions: 5 8 62.5 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2001, 2003, 2005 - 2020 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  *
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  *
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  *
      17             :  * 3. Neither the name of the Institute nor the names of its contributors
      18             :  *    may be used to endorse or promote products derived from this software
      19             :  *    without specific prior written permission.
      20             :  *
      21             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      22             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      25             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      26             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      27             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      28             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      29             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      30             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      31             :  * SUCH DAMAGE.
      32             :  */
      33             : 
      34             : #include "krb5_locl.h"
      35             : 
      36             : #undef __attribute__
      37             : #define __attribute__(x)
      38             : 
      39             : /**
      40             :  * Clears the error message from the Kerberos 5 context.
      41             :  *
      42             :  * @param context The Kerberos 5 context to clear
      43             :  *
      44             :  * @ingroup krb5_error
      45             :  */
      46             : 
      47             : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
      48     1499013 : krb5_clear_error_message(krb5_context context)
      49             : {
      50     1499013 :     heim_clear_error_message(context->hcontext);
      51     1499013 : }
      52             : 
      53             : /**
      54             :  * Set the context full error string for a specific error code.
      55             :  * The error that is stored should be internationalized.
      56             :  *
      57             :  * The if context is NULL, no error string is stored.
      58             :  *
      59             :  * @param context Kerberos 5 context
      60             :  * @param ret The error code
      61             :  * @param fmt Error string for the error code
      62             :  * @param ... printf(3) style parameters.
      63             :  *
      64             :  * @ingroup krb5_error
      65             :  */
      66             : 
      67             : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
      68     1174466 : krb5_set_error_message(krb5_context context, krb5_error_code ret,
      69             :                        const char *fmt, ...)
      70             :     __attribute__ ((__format__ (__printf__, 3, 4)))
      71             : {
      72       23270 :     va_list ap;
      73             : 
      74     1174466 :     va_start(ap, fmt);
      75     1174466 :     krb5_vset_error_message (context, ret, fmt, ap);
      76     1174466 :     va_end(ap);
      77     1174466 : }
      78             : 
      79             : /**
      80             :  * Set the context full error string for a specific error code.
      81             :  *
      82             :  * The if context is NULL, no error string is stored.
      83             :  *
      84             :  * @param context Kerberos 5 context
      85             :  * @param ret The error code
      86             :  * @param fmt Error string for the error code
      87             :  * @param args printf(3) style parameters.
      88             :  *
      89             :  * @ingroup krb5_error
      90             :  */
      91             : 
      92             : 
      93             : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
      94     1174466 : krb5_vset_error_message(krb5_context context, krb5_error_code ret,
      95             :                         const char *fmt, va_list args)
      96             :     __attribute__ ((__format__ (__printf__, 3, 0)))
      97             : {
      98       23270 :     const char *msg;
      99             : 
     100     1174466 :     if (context == NULL)
     101           0 :         return;
     102             : 
     103     1174466 :     heim_vset_error_message(context->hcontext, ret, fmt, args);
     104     1174466 :     msg = heim_get_error_message(context->hcontext, ret);
     105     1174466 :     if (msg) {
     106     1174466 :         _krb5_debug(context, 100, "error message: %s: %d", msg, ret);
     107     1174466 :         heim_free_error_message(context->hcontext, msg);
     108             :     }
     109             : }
     110             : 
     111             : /**
     112             :  * Prepend the context full error string for a specific error code.
     113             :  * The error that is stored should be internationalized.
     114             :  *
     115             :  * The if context is NULL, no error string is stored.
     116             :  *
     117             :  * @param context Kerberos 5 context
     118             :  * @param ret The error code
     119             :  * @param fmt Error string for the error code
     120             :  * @param ... printf(3) style parameters.
     121             :  *
     122             :  * @ingroup krb5_error
     123             :  */
     124             : 
     125             : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
     126           0 : krb5_prepend_error_message(krb5_context context, krb5_error_code ret,
     127             :                            const char *fmt, ...)
     128             :     __attribute__ ((__format__ (__printf__, 3, 4)))
     129             : {
     130           0 :     va_list ap;
     131             : 
     132           0 :     va_start(ap, fmt);
     133           0 :     krb5_vprepend_error_message(context, ret, fmt, ap);
     134           0 :     va_end(ap);
     135           0 : }
     136             : 
     137             : /**
     138             :  * Prepend the contexts's full error string for a specific error code.
     139             :  *
     140             :  * The if context is NULL, no error string is stored.
     141             :  *
     142             :  * @param context Kerberos 5 context
     143             :  * @param ret The error code
     144             :  * @param fmt Error string for the error code
     145             :  * @param args printf(3) style parameters.
     146             :  *
     147             :  * @ingroup krb5_error
     148             :  */
     149             : 
     150             : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
     151           0 : krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
     152             :                             const char *fmt, va_list args)
     153             :     __attribute__ ((__format__ (__printf__, 3, 0)))
     154             : {
     155           0 :     if (context)
     156           0 :         heim_vprepend_error_message(context->hcontext, ret, fmt, args);
     157           0 : }
     158             : 
     159             : /**
     160             :  * Return the error message for `code' in context. On memory
     161             :  * allocation error the function returns NULL.
     162             :  *
     163             :  * @param context Kerberos 5 context
     164             :  * @param code Error code related to the error
     165             :  *
     166             :  * @return an error string, needs to be freed with
     167             :  * krb5_free_error_message(). The functions return NULL on error.
     168             :  *
     169             :  * @ingroup krb5_error
     170             :  */
     171             : 
     172             : KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
     173        4884 : krb5_get_error_message(krb5_context context, krb5_error_code code)
     174             : {
     175        4884 :     const char *cstr = NULL;
     176             : 
     177        4884 :     if (code == 0)
     178           0 :         return strdup("Success");
     179             : 
     180             :     /*
     181             :      * The MIT version of this function ignores the krb5_context
     182             :      * and several widely deployed applications call krb5_get_error_message()
     183             :      * with a NULL context in order to translate an error code as a
     184             :      * replacement for error_message().  Another reason a NULL context
     185             :      * might be provided is if the krb5_init_context() call itself
     186             :      * failed.
     187             :      */
     188        4884 :     if (context == NULL && krb5_init_context(&context) == 0) {
     189           0 :         cstr = heim_get_error_message(context->hcontext, code);
     190           0 :         krb5_free_context(context);
     191        4884 :     } else if (context) {
     192        4884 :         cstr = heim_get_error_message(context->hcontext, code);
     193             :     } else {
     194           0 :         cstr = heim_get_error_message(NULL, code);
     195             :     }
     196        4884 :     return cstr;
     197             : }
     198             : 
     199             : 
     200             : /**
     201             :  * Free the error message returned by krb5_get_error_message().
     202             :  *
     203             :  * @param context Kerberos context
     204             :  * @param msg error message to free, returned byg
     205             :  *        krb5_get_error_message().
     206             :  *
     207             :  * @ingroup krb5_error
     208             :  */
     209             : 
     210             : KRB5_LIB_FUNCTION void KRB5_LIB_CALL
     211        4884 : krb5_free_error_message(krb5_context context, const char *msg)
     212             : {
     213        4884 :     heim_free_error_message(context ? context->hcontext : NULL, msg);
     214        4884 : }
     215             : 
     216             : 
     217             : /**
     218             :  * Return the error string for the error code. The caller must not
     219             :  * free the string.
     220             :  *
     221             :  * This function is deprecated since its not threadsafe.
     222             :  *
     223             :  * @param context Kerberos 5 context.
     224             :  * @param code Kerberos error code.
     225             :  *
     226             :  * @return the error message matching code
     227             :  *
     228             :  * @ingroup krb5
     229             :  */
     230             : 
     231             : KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
     232           0 : krb5_get_err_text(krb5_context context, krb5_error_code code)
     233             :     KRB5_DEPRECATED_FUNCTION("Use krb5_get_error_message instead")
     234             : {
     235           0 :     return krb5_get_error_message(context, code);
     236             : }

Generated by: LCOV version 1.14