Skip to content
0.2.0-dev Download ZIP
123 lines ·3.5 KB Raw
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#include "stdlib/SDL_vacopy.h"
24
25// Simple error handling in SDL
26
27#include "SDL_error_c.h"
28
29bool SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
30{
31 va_list ap;
32 bool result;
33
34 va_start(ap, fmt);
35 result = SDL_SetErrorV(fmt, ap);
36 va_end(ap);
37 return result;
38}
39
40bool SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
41{
42 // Ignore call if invalid format pointer was passed
43 if (fmt) {
44 int result;
45 SDL_error *error = SDL_GetErrBuf(true);
46
47 // use the other slot for the new error, so if this does
48 // SDL_SetError("%s", SDL_GetError()), we don't have a problem.
49 const int current = error->current ? 0 : 1;
50 SDL_ErrorInfo *errinfo = &error->info[current];
51 error->current = current;
52
53 errinfo->error = SDL_ErrorCodeGeneric;
54
55 va_list ap2;
56 va_copy(ap2, ap);
57 result = SDL_vsnprintf(errinfo->str, errinfo->len, fmt, ap2);
58 va_end(ap2);
59
60 if (result >= 0 && (size_t)result >= errinfo->len && error->realloc_func) {
61 size_t len = (size_t)result + 1;
62 char *str = (char *)error->realloc_func(errinfo->str, len);
63 if (str) {
64 errinfo->str = str;
65 errinfo->len = len;
66 va_copy(ap2, ap);
67 (void)SDL_vsnprintf(errinfo->str, errinfo->len, fmt, ap2);
68 va_end(ap2);
69 }
70 }
71
72// Enable this if you want to see all errors printed as they occur.
73// Note that there are many recoverable errors that may happen internally and
74// can be safely ignored if the public API doesn't return an error code.
75#if 0
76 SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", errinfo->str);
77#endif
78 }
79
80 return false;
81}
82
83const char *SDL_GetError(void)
84{
85 const SDL_error *error = SDL_GetErrBuf(false);
86
87 if (!error) {
88 return "";
89 }
90
91 const SDL_ErrorInfo *errinfo = &error->info[error->current];
92 switch (errinfo->error) {
93 case SDL_ErrorCodeGeneric:
94 return errinfo->str;
95 case SDL_ErrorCodeOutOfMemory:
96 return "Out of memory";
97 default:
98 return "";
99 }
100}
101
102bool SDL_ClearError(void)
103{
104 SDL_error *error = SDL_GetErrBuf(false);
105
106 if (error) {
107 SDL_ErrorInfo *errinfo = &error->info[error->current];
108 errinfo->error = SDL_ErrorCodeNone;
109 }
110 return true;
111}
112
113bool SDL_OutOfMemory(void)
114{
115 SDL_error *error = SDL_GetErrBuf(true);
116
117 if (error) {
118 SDL_ErrorInfo *errinfo = &error->info[error->current];
119 errinfo->error = SDL_ErrorCodeOutOfMemory;
120 }
121 return false;
122}
123