Skip to content

Commit 0dcade2

Browse files
tdp2110rajeevsrao
authored andcommitted
don't violate strict aliasing with reinterpret_cast
Signed-off-by: Tom Peters <[email protected]>
1 parent 4e5e724 commit 0dcade2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Diff for: plugin/common/plugin.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "checkMacrosPlugin.h"
2020

2121
#include "NvInferPlugin.h"
22+
#include <cstring>
2223
#include <cuda_runtime.h>
2324
#include <iostream>
2425
#include <memory>
@@ -78,15 +79,16 @@ class BaseCreator : public IPluginCreator
7879
template <typename T>
7980
void write(char*& buffer, const T& val)
8081
{
81-
*reinterpret_cast<T*>(buffer) = val;
82+
std::memcpy(buffer, &val, sizeof(T));
8283
buffer += sizeof(T);
8384
}
8485

8586
// Read values from buffer
8687
template <typename T>
8788
T read(const char*& buffer)
8889
{
89-
T val = *reinterpret_cast<const T*>(buffer);
90+
T val;
91+
std::memcpy(&val, buffer, sizeof(T));
9092
buffer += sizeof(T);
9193
return val;
9294
}

Diff for: samples/opensource/sampleUffPluginV2Ext/sampleUffPluginV2Ext.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
#include "NvUffParser.h"
1919
#include <cassert>
2020
#include <chrono>
21+
#include <cstring>
2122
#include <cudnn.h>
2223
#include <iostream>
2324
#include <map>
24-
#include <string.h>
2525
#include <unordered_map>
2626
#include <vector>
2727

@@ -579,14 +579,15 @@ class UffPoolPluginV2 : public IPluginV2IOExt
579579
template <typename T>
580580
void write(char*& buffer, const T& val) const
581581
{
582-
*reinterpret_cast<T*>(buffer) = val;
582+
std::memcpy(buffer, &val, sizeof(T));
583583
buffer += sizeof(T);
584584
}
585585

586586
template <typename T>
587587
T read(const char*& buffer) const
588588
{
589-
T val = *reinterpret_cast<const T*>(buffer);
589+
T val;
590+
std::memcpy(&val, buffer, sizeof(T));
590591
buffer += sizeof(T);
591592
return val;
592593
}

0 commit comments

Comments
 (0)