Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/runtime_src/core/common/api/xrt_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
#include <map>
#include <cstring>

#ifdef _WIN32
# pragma warning( disable : 4996)
#endif

namespace {

static auto code_to_string = [] (auto& map, auto code, const std::string& msg)
Expand Down Expand Up @@ -354,7 +350,11 @@ xrtErrorGetString(xrtDeviceHandle, xrtErrorCode error, char* out, size_t len, si
return 0;

auto cp_len = std::min(len-1, str.size());
#ifdef _WIN32
strncpy_s(out, len, str.c_str(), cp_len);
#else
std::strncpy(out, str.c_str(), cp_len);
#endif
out[cp_len] = 0;

return 0;
Expand Down
106 changes: 106 additions & 0 deletions src/runtime_src/core/common/str_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

// Cross-platform wrapper for banned C string APIs
// Provides secure replacements for Windows driver compliance

#ifndef SAFE_STR_WRAPPER_H_
#define SAFE_STR_WRAPPER_H_
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

#include <errno.h>
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
#include <stddef.h>
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
#include <stdio.h>
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
#include <string.h>
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
#include <cstring>
#include <stdarg.h>
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

#ifdef _WIN32

#undef strcpy // No bounds checking
#define strcpy(dest, src) strcpy_s((dest), sizeof(dest), (src))
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

#undef strncpy // Limited error detection
#define strncpy(dest, src, n) strncpy_s((dest), sizeof(dest), (src), (n))

#undef strcat // Limited error detection
#define strcat(dest, src) strcat_s((dest), sizeof(dest), (src))

#undef strncat // Limited error detection
#define strncat(dest, src, n) strncat_s((dest), sizeof(dest), (src), (n))

#undef sprintf // Limited error detection
#define sprintf(buf, format, ...) sprintf_s((buf), sizeof(buf), (format), ##__VA_ARGS__)

#undef vsnprintf // Limited error detection
#define vsnprintf(buf, size, format, args) vsnprintf_s((buf), sizeof(buf), (size), (format), (args))

#else
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
namespace xrt_str_safe {

inline errno_t strcpy_safe(char* dest, size_t dest_size, const char* src) {
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
if (!dest || !src || dest_size == 0) return EINVAL;
size_t src_len = strlen(src);
if (src_len >= dest_size) return ERANGE;
std::strcpy(dest, src);
return 0;
}

inline errno_t strncpy_safe(char* dest, size_t dest_size, const char* src, size_t count) {
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
if (!dest || !src || dest_size == 0) return EINVAL;
size_t copy_len = (count < dest_size - 1) ? count : dest_size - 1;
std::strncpy(dest, src, copy_len);
dest[copy_len] = '\0';
return 0;
}

inline errno_t strcat_safe(char* dest, size_t dest_size, const char* src) {
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
if (!dest || !src || dest_size == 0) return EINVAL;
size_t dest_len = strlen(dest);
size_t src_len = strlen(src);
if (dest_len + src_len >= dest_size) return ERANGE;
std::strcat(dest, src);
return 0;
}

inline errno_t strncat_safe(char* dest, size_t dest_size, const char* src, size_t count) {
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
if (!dest || !src || dest_size == 0) return EINVAL;
size_t dest_len = strlen(dest);
size_t available = dest_size - dest_len - 1;
size_t copy_len = (count < available) ? count : available;
std::strncat(dest, src, copy_len);
return 0;
}

} // namespace xrt_str_safe

#undef strcpy
#define strcpy(dest, src) (xrt_str_safe::strcpy_safe((dest), sizeof(dest), (src)), (dest))
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

#undef strncpy
#define strncpy(dest, src, n) (xrt_str_safe::strncpy_safe((dest), sizeof(dest), (src), (n)), (dest))
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

#undef strcat
#define strcat(dest, src) (xrt_str_safe::strcat_safe((dest), sizeof(dest), (src)), (dest))
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

#undef strncat
#define strncat(dest, src, n) (xrt_str_safe::strncat_safe((dest), sizeof(dest), (src), (n)), (dest))
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

#undef sprintf
#define sprintf(buf, format, ...) std::snprintf((buf), sizeof(buf), (format), ##__VA_ARGS__)
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated

#endif // _WIN32

#endif // SAFE_STR_WRAPPER_H_
Loading