2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
9 * @file safeguards.h A number of safeguards to prevent using unsafe methods.
11 * Unsafe methods are, for example, strndup and strncpy because they may leave the
12 * string without a null termination, but also strdup and strndup because they can
13 * return nullptr and then all strdups would need to be guarded against that instead
14 * of using the current MallocT/ReallocT/CallocT technique of just giving the user
15 * an error that too much memory was used instead of spreading that code though
16 * the whole code base.
22 /* Use MallocT instead. */
23 #define malloc SAFEGUARD_DO_NOT_USE_THIS_METHOD
25 /* Use MallocT instead. */
26 #define calloc SAFEGUARD_DO_NOT_USE_THIS_METHOD
28 /* Use ReallocT instead. */
29 #define realloc SAFEGUARD_DO_NOT_USE_THIS_METHOD
31 /* Use std::string instead. */
32 #define strdup SAFEGUARD_DO_NOT_USE_THIS_METHOD
33 #define strndup SAFEGUARD_DO_NOT_USE_THIS_METHOD
35 /* Use strecpy instead. */
36 #define strcpy SAFEGUARD_DO_NOT_USE_THIS_METHOD
37 #define strncpy SAFEGUARD_DO_NOT_USE_THIS_METHOD
39 /* Use std::string concatenation/fmt::format instead. */
40 #define strcat SAFEGUARD_DO_NOT_USE_THIS_METHOD
41 #define strncat SAFEGUARD_DO_NOT_USE_THIS_METHOD
43 /* Use fmt::format instead. */
44 #define sprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
45 #define snprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
47 /* Use fmt::format instead. */
48 #define vsprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
49 #define vsnprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
51 /* Use fgets instead. */
52 #define gets SAFEGUARD_DO_NOT_USE_THIS_METHOD
54 /* No clear replacement. */
55 #define strtok SAFEGUARD_DO_NOT_USE_THIS_METHOD
57 /* Use fmt::print instead. */
58 #define printf SAFEGUARD_DO_NOT_USE_THIS_METHOD
59 #define fprintf SAFEGUARD_DO_NOT_USE_THIS_METHOD
60 #define puts SAFEGUARD_DO_NOT_USE_THIS_METHOD
61 #define fputs SAFEGUARD_DO_NOT_USE_THIS_METHOD
62 #define putchar SAFEGUARD_DO_NOT_USE_THIS_METHOD
64 /* Use our own templated implementation instead of a macro or function with only one type. */
69 /* Use our own templated implementation instead of a macro or function with only one type. */
74 /* Use our own templated implementation instead of a macro or function with only one type. */
79 #if defined(NETWORK_CORE_OS_ABSTRACTION_H) && defined(_WIN32)
80 /* Use NetworkError::GetLast() instead of errno, or do not (indirectly) include network/core/os_abstraction.h.
81 * Winsock does not set errno, but one should rather call WSAGetLastError. NetworkError::GetLast abstracts that away. */
85 #define errno SAFEGUARD_DO_NOT_USE_THIS_METHOD
87 /* Use NetworkError::AsString() instead of strerror, or do not (indirectly) include network/core/os_abstraction.h.
88 * Winsock errors are not handled by strerror, but one should rather call FormatMessage. NetworkError::AsString abstracts that away. */
89 #define strerror SAFEGUARD_DO_NOT_USE_THIS_METHOD
90 #endif /* defined(NETWORK_CORE_OS_ABSTRACTION_H) && defined(_WIN32) */
92 #endif /* SAFEGUARDS_H */