如何解决现有 Crypto++ 代码中的奇怪错误,将指针转换为 void*
使用 Crypto++ 编译时出现奇怪的转换错误
(Crypto++ 源代码由我的应用程序直接 #include
,在同一个程序集中。
所有 Crypto++ *.cpp
源文件都添加到 CMake 中并直接编译。这通常工作正常。)
/*******************************************
** Snippet of unedited Crypto++ source code:
** misc.h
*******************************************/
template <class T>
inline void SecureWipeArray(T *buf,size_t n)
{
if (sizeof(T) % 8 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word64>() == 0)
SecureWipeBuffer(reinterpret_cast<word64 *>(static_cast<void *>(buf)),n * (sizeof(T)/8)); //error
else if (sizeof(T) % 4 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word32>() == 0)
SecureWipeBuffer(reinterpret_cast<word32 *>(static_cast<void *>(buf)),n * (sizeof(T)/4)); //error
else if (sizeof(T) % 2 == 0 && GetAlignmentOf<T>() % GetAlignmentOf<word16>() == 0)
SecureWipeBuffer(reinterpret_cast<word16 *>(static_cast<void *>(buf)),n * (sizeof(T)/2)); //error
else
SecureWipeBuffer(reinterpret_cast<byte *>(static_cast<void *>(buf)),n * sizeof(T)); //error
}
所有四行都给出相同的错误:
'static_cast': 不能从 'T *' 转换为 'void *'
'CryptoPP::SecureWipeBuffer':找不到匹配的重载函数
我正在使用 C++20 进行编译,但是在编译 C++20 之前的代码时,这不会导致破坏性问题,对吗?
我写了一个“SecArray”类,它继承了 std::array
并在对象被销毁时使用 SecureWipeArray
。
(旁注:析构函数应该是虚拟的吗?std::array
没有任何析构函数)
namespace myNamespace
{
template<class T,size_t length>
class array : public std::array<T,length>
{
private:
using base = std::array<T,length>;
public:
inline ~array() noexcept
{
SecureWipeArray(base::data(),length);
}
constexpr inline operator T* () noexcept { return base::data(); }
constexpr inline operator T const* () const noexcept { return base::data(); }
};
}
这是我的代码唯一一次直接调用 SecureWipeArray
。
(我还编写了一些容器类,它们使用带有 std
的 CryptoPP::AllocatorWithCleanup
容器,后者又调用 SecureWipeArray
)
但是 T
应该仍然是基本类型,例如 char
、uint
、string
等
所以我不确定是什么导致了这个错误。
我应该在哪里寻找潜在原因?
解决方法
如果您有一个 const 的 std 数组,任何修改其内容的尝试都是未定义的行为。
这意味着您的程序在写入之前和之后的行为不受 C++ 标准的限制。是的,UB 可以导致时间旅行,而且确实如此。
最可能的影响是写入未发生,并非所有代码都能看到写入(以不一致的方式),和/或数据位于受保护的页面或 rom 上,并且您的程序或计算机崩溃。
C++ 中的常量数据(相对于常量指针)实际上是常量。
如果你想这样做,去掉奇怪中的const并为其提供一个const接口。那么你想做的就是合法的。
但是,不要使用联合来输入双关语。这在 C++ 中的大多数情况下都是不合法的。
template<class T,size_t length>
struct array<T const,length> : private std::array<T,length>
{
using base=std::array<T,length>;
// then forward:
T const*begin()const{return base::begin();}
// etc
array(std::array<T const,length> arr):base(arr){}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。