Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't violate strict aliasing with reinterpret_cast #146

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions plugin/common/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef TRT_PLUGIN_H
#define TRT_PLUGIN_H
#include "NvInferPlugin.h"
#include <cstring>
#include <cuda_runtime.h>
#include <iostream>
#include <memory>
Expand Down Expand Up @@ -57,15 +58,16 @@ class BaseCreator : public IPluginCreator
template <typename T>
void write(char*& buffer, const T& val)
{
*reinterpret_cast<T*>(buffer) = val;
std::memcpy(buffer, &val, sizeof(T));
buffer += sizeof(T);
}

// Read values from buffer
template <typename T>
T read(const char*& buffer)
{
T val = *reinterpret_cast<const T*>(buffer);
T val;
std::memcpy(&val, buffer, sizeof(T));
buffer += sizeof(T);
return val;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
#include "NvUffParser.h"
#include <cassert>
#include <chrono>
#include <cstring>
#include <cudnn.h>
#include <iostream>
#include <map>
#include <string.h>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -579,14 +579,15 @@ class UffPoolPluginV2 : public IPluginV2IOExt
template <typename T>
void write(char*& buffer, const T& val) const
{
*reinterpret_cast<T*>(buffer) = val;
std::memcpy(buffer, &val, sizeof(T));
buffer += sizeof(T);
}

template <typename T>
T read(const char*& buffer) const
{
T val = *reinterpret_cast<const T*>(buffer);
T val;
std::memcpy(&val, buffer, sizeof(T));
buffer += sizeof(T);
return val;
}
Expand Down