Nuitka
The Python compiler
Loading...
Searching...
No Matches
safe_string_ops.h
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_SAFE_STRING_OPS_H__
4#define __NUITKA_SAFE_STRING_OPS_H__
5
6#include <errno.h>
7#include <stdbool.h>
8#include <stddef.h>
9#include <stdint.h>
10
11/* Safe to use function to copy a string, will abort program for overflow. */
12extern void copyStringSafe(char *buffer, char const *source, size_t buffer_size);
13extern void copyStringSafeN(char *buffer, char const *source, size_t n, size_t buffer_size);
14extern void copyStringSafeW(wchar_t *buffer, wchar_t const *source, size_t buffer_size);
15
16/* Safe to use function to append a string, will abort program for overflow. */
17extern void appendCharSafe(char *target, char c, size_t buffer_size);
18extern void appendStringSafe(char *target, char const *source, size_t buffer_size);
19
20/* Safe to use functions to append a wide char string, will abort program for overflow. */
21extern void appendCharSafeW(wchar_t *target, char c, size_t buffer_size);
22extern void appendWCharSafeW(wchar_t *target, wchar_t c, size_t buffer_size);
23extern void appendStringSafeW(wchar_t *target, char const *source, size_t buffer_size);
24extern void appendWStringSafeW(wchar_t *target, wchar_t const *source, size_t buffer_size);
25
26// Check that a string value is actually a number, used to prevent path
27// injections with inherited environment variables.
28void checkWStringNumber(wchar_t const *value);
29void checkStringNumber(char const *value);
30
31/* Get OS error code and print it to stderr. */
32#ifdef _WIN32
33typedef DWORD error_code_t;
34#define ERROR_CODE_FORMAT_STR "%ld"
35static inline error_code_t getCurrentErrorCode(void) { return GetLastError(); }
36#else
37typedef int error_code_t;
38#define ERROR_CODE_FORMAT_STR "%d"
39static inline error_code_t getCurrentErrorCode(void) { return errno; }
40#endif
41extern void printOSErrorMessage(char const *message, error_code_t error_code);
42
43#endif
44// Part of "Nuitka", an optimizing Python compiler that is compatible and
45// integrates with CPython, but also works on its own.
46//
47// Licensed under the Apache License, Version 2.0 (the "License");
48// you may not use this file except in compliance with the License.
49// You may obtain a copy of the License at
50//
51// http://www.apache.org/licenses/LICENSE-2.0
52//
53// Unless required by applicable law or agreed to in writing, software
54// distributed under the License is distributed on an "AS IS" BASIS,
55// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
56// See the License for the specific language governing permissions and
57// limitations under the License.