Coverage Report

Created: 2026-12-01 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/src/error.c
Line
Count
Source
1
/*
2
 * error.c - functions for ssh error handling
3
 *
4
 * This file is part of the SSH Library
5
 *
6
 * Copyright (c) 2003-2008 by Aris Adamantiadis
7
 *
8
 * The SSH Library is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or (at your
11
 * option) any later version.
12
 *
13
 * The SSH Library is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16
 * License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with the SSH Library; see the file COPYING.  If not, write to
20
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21
 * MA 02111-1307, USA.
22
 */
23
24
#include "config.h"
25
26
#include <stdio.h>
27
#include <stdarg.h>
28
#include "libssh/priv.h"
29
#include "libssh/session.h"
30
31
/**
32
 * @defgroup libssh_error The SSH error functions
33
 * @ingroup libssh
34
 *
35
 * Functions for error handling.
36
 *
37
 * @{
38
 */
39
40
/**
41
 * @internal
42
 *
43
 * @brief Registers an error with a description.
44
 *
45
 * @param  error       The place to store the error.
46
 * @param  code        The class of error.
47
 * @param  function    The name of the calling function.
48
 * @param  descr       The description, which can be a format string.
49
 * @param  ...         The arguments for the format string.
50
 */
51
void _ssh_set_error(void *error,
52
                    int code,
53
                    const char *function,
54
                    const char *descr, ...)
55
711
{
56
711
    struct ssh_common_struct *err = error;
57
711
    va_list va;
58
59
711
    va_start(va, descr);
60
711
    vsnprintf(err->error.error_buffer, ERROR_BUFFERLEN, descr, va);
61
711
    va_end(va);
62
63
711
    err->error.error_code = code;
64
711
    if (ssh_get_log_level() == SSH_LOG_TRACE) {
65
0
        ssh_log_function(SSH_LOG_TRACE,
66
0
                         function,
67
0
                         err->error.error_buffer);
68
0
    }
69
711
}
70
71
/**
72
 * @internal
73
 *
74
 * @brief Registers an out of memory error
75
 *
76
 * @param  error       The place to store the error.
77
 * @param  function    The name of the calling function.
78
 *
79
 */
80
void _ssh_set_error_oom(void *error, const char *function)
81
0
{
82
0
    struct error_struct *err = error;
83
84
0
    snprintf(err->error_buffer, sizeof(err->error_buffer),
85
0
            "%s: Out of memory", function);
86
0
    err->error_code = SSH_FATAL;
87
0
}
88
89
/**
90
 * @internal
91
 *
92
 * @brief Registers an invalid argument error
93
 *
94
 * @param  error       The place to store the error.
95
 *
96
 * @param  function    The function the error happened in.
97
 *
98
 */
99
void _ssh_set_error_invalid(void *error, const char *function)
100
0
{
101
0
    _ssh_set_error(error, SSH_FATAL, function,
102
0
                   "Invalid argument in %s", function);
103
0
}
104
105
/**
106
 * @internal
107
 *
108
 * @brief Reset the error code and message
109
 *
110
 * @param  error       The place to reset the error.
111
 */
112
void ssh_reset_error(void *error)
113
237
{
114
237
    struct ssh_common_struct *err = error;
115
116
237
    ZERO_STRUCT(err->error.error_buffer);
117
237
    err->error.error_code = 0;
118
237
}
119
120
/**
121
 * @brief Retrieve the error text message from the last error.
122
 *
123
 * @param  error        An ssh_session or ssh_bind.
124
 *
125
 * @return A static string describing the error.
126
 */
127
474
const char *ssh_get_error(void *error) {
128
474
  struct error_struct *err = error;
129
130
474
  return err->error_buffer;
131
474
}
132
133
/**
134
 * @brief Retrieve the error code from the last error.
135
 *
136
 * @param  error        An ssh_session or ssh_bind.
137
 *
138
 * \return SSH_NO_ERROR       No error occurred\n
139
 *         SSH_REQUEST_DENIED The last request was denied but situation is
140
 *                            recoverable\n
141
 *         SSH_FATAL          A fatal error occurred. This could be an unexpected
142
 *                            disconnection\n
143
 *
144
 *         Other error codes are internal but can be considered the same as
145
 *         SSH_FATAL.
146
 */
147
0
int ssh_get_error_code(void *error) {
148
0
  struct error_struct *err = error;
149
150
0
  return err->error_code;
151
0
}
152
153
/** @} */