Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 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
7 changes: 2 additions & 5 deletions src/runtime_src/core/common/api/xrt_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "core/common/system.h"
#include "core/common/device.h"
#include "core/common/message.h"
#include "core/common/str_wrapper.h"
#include "core/common/query_requests.h"

#include <boost/format.hpp>
Expand All @@ -38,10 +39,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 +351,7 @@ xrtErrorGetString(xrtDeviceHandle, xrtErrorCode error, char* out, size_t len, si
return 0;

auto cp_len = std::min(len-1, str.size());
std::strncpy(out, str.c_str(), cp_len);
xrt_core::str_wrapper::strncpy(out, str.c_str(), cp_len);
Comment thread
hyunjiki-amd marked this conversation as resolved.
Outdated
out[cp_len] = 0;

return 0;
Expand Down
45 changes: 45 additions & 0 deletions src/runtime_src/core/common/str_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.

#include "core/common/str_wrapper.h"

#include <cstdio>
#include <cstring>
#include <cstdarg>

namespace xrt_core { namespace str_wrapper {
Comment thread
stsoe marked this conversation as resolved.
Outdated
Comment thread
stsoe marked this conversation as resolved.
Outdated
// Pointer case - use std:: functions (cross-platform)
char* strcpy(char* dest, const char* src)
{
return std::strcpy(dest, src);
Comment thread
stsoe marked this conversation as resolved.
Outdated
Comment thread
stsoe marked this conversation as resolved.
Outdated
}

char* strncpy(char* dest, const char* src, size_t n)
{
return std::strncpy(dest, src, n);
}

char* strcat(char* dest, const char* src)
{
return std::strcat(dest, src);
Comment thread
stsoe marked this conversation as resolved.
Outdated
Comment thread
stsoe marked this conversation as resolved.
Outdated
}

char* strncat(char* dest, const char* src, size_t n)
{
return std::strncat(dest, src, n);
}

int sprintf(char* buf, const char* format, ...)
Comment thread
stsoe marked this conversation as resolved.
Outdated
{
va_list args;
Comment thread
stsoe marked this conversation as resolved.
Outdated
va_start(args, format);
int result = std::vsprintf(buf, format, args);
va_end(args);
return result;
}

int vsnprintf(char* buf, size_t size, const char* format, va_list args)
{
return std::vsnprintf(buf, size, format, args);
}
}} // str_wrapper, xrt_core
62 changes: 62 additions & 0 deletions src/runtime_src/core/common/str_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.

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

#ifndef XRT_CORE_COMMON_SAFE_STR_H
#define XRT_CORE_COMMON_SAFE_STR_H

#include <cstddef>
#include <cstdio>
#include <cstring>
#include <cstdarg>

namespace xrt_core { namespace str_wrapper {
Comment thread
stsoe marked this conversation as resolved.
Outdated
Comment thread
stsoe marked this conversation as resolved.
Outdated
// Array case - use secure *_s functions (Windows only)
#ifdef _WIN32
template<size_t N>
inline char* strcpy(char (&dest)[N], const char* src) {
return strcpy_s(dest, N, src) == 0 ? dest : nullptr;
}

template<size_t N>
inline char* strncpy(char (&dest)[N], const char* src, size_t n) {
return strncpy_s(dest, N, src, n) == 0 ? dest : nullptr;
}

template<size_t N>
inline char* strcat(char (&dest)[N], const char* src) {
return strcat_s(dest, N, src) == 0 ? dest : nullptr;
}

template<size_t N>
inline char* strncat(char (&dest)[N], const char* src, size_t n) {
return strncat_s(dest, N, src, n) == 0 ? dest : nullptr;
}

template<size_t N>
inline int sprintf(char (&buf)[N], const char* format, ...) {
va_list args;
va_start(args, format);
int result = vsprintf_s(buf, N, format, args);
va_end(args);
return result;
}

template<size_t N>
inline int vsnprintf(char (&buf)[N], size_t size, const char* format, va_list args) {
return vsnprintf_s(buf, N, size, format, args);
}
#endif

// Pointer case - use std:: functions (cross-platform)
char* strcpy(char* dest, const char* src);
char* strncpy(char* dest, const char* src, size_t n);
char* strcat(char* dest, const char* src);
char* strncat(char* dest, const char* src, size_t n);
int sprintf(char* buf, const char* format, ...);
int vsnprintf(char* buf, size_t size, const char* format, va_list args);
}} // str_wrapper, xrt_core

#endif // XRT_CORE_COMMON_SAFE_STR_H
Loading