1 /*
2  * PROJECT:     GCC c++ support library
3  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
4  * PURPOSE:     __throw_out_of_range_fmt implementation
5  * COPYRIGHT:   Copyright 2024 Timo Kreuzer <timo.kreuzer@reactos.org>
6  */
7 
8 #include <stdexcept>
9 #include <cstdarg>
10 #include <cstring>
11 #include <malloc.h>
12 
13 namespace std {
14 
15 class out_of_range_error : public exception
16 {
17     char* m_msg;
18 public:
19     explicit out_of_range_error(const char* msg) noexcept
20     {
21         size_t len = strlen(msg) + 1;
22         m_msg = (char*)malloc(len);
23         strcpy(m_msg, msg);
24     }
25     virtual ~out_of_range_error() { free(m_msg); };
26     virtual const char* what() const noexcept { return m_msg; }
27 };
28 
29 void __throw_out_of_range_fmt(const char *format, ...)
30 {
31     char buffer[1024];
32     va_list argptr;
33 
34     va_start(argptr, format);
35     _vsnprintf(buffer, sizeof(buffer), format, argptr);
36     buffer[sizeof(buffer) - 1] = 0;
37     va_end(argptr);
38 
39     throw out_of_range_error(buffer);
40 }
41 
42 }  // namespace std
43