Fix tools, cleanup, remove reinterpret_cast undefined behaviour

pull/1149/head
Andrew Tribick 2021-10-30 00:29:37 +02:00 committed by ajtribick
parent 61850bfb9b
commit e00f5b7d2c
3 changed files with 19 additions and 13 deletions

View File

@ -9,6 +9,7 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
@ -17,7 +18,7 @@
#include <Eigen/Core>
#include <fmt/ostream.h>
#include "celutil/bytes.h"
#include <celutil/bytes.h>
#include "3dschunk.h"
#include "3dsmodel.h"
#include "3dsread.h"
@ -34,8 +35,9 @@ using ProcessChunkFunc = std::int32_t (*)(std::istream &, std::uint16_t, std::in
bool readInt(std::istream& in, std::int32_t& value)
{
in.read(reinterpret_cast<char*>(&value), sizeof(std::int32_t));
if (!in.good()) { return false; }
char buffer[sizeof(value)];
if (!in.read(buffer, sizeof(buffer)).good()) { return false; }
std::memcpy(&value, buffer, sizeof(value));
LE_TO_CPU_INT32(value, value);
return true;
}
@ -43,8 +45,9 @@ bool readInt(std::istream& in, std::int32_t& value)
bool readShort(std::istream& in, std::int16_t& value)
{
in.read(reinterpret_cast<char*>(&value), sizeof(std::int16_t));
if (!in.good()) { return false; }
char buffer[sizeof(value)];
if (!in.read(buffer, sizeof(buffer)).good()) { return false; }
std::memcpy(&value, buffer, sizeof(value));
LE_TO_CPU_INT16(value, value);
return true;
}
@ -52,8 +55,9 @@ bool readShort(std::istream& in, std::int16_t& value)
bool readUshort(std::istream& in, std::uint16_t& value)
{
in.read(reinterpret_cast<char*>(&value), sizeof(std::uint16_t));
if (!in.good()) { return false; }
char buffer[sizeof(value)];
if (!in.read(buffer, sizeof(buffer)).good()) { return false; }
std::memcpy(&value, buffer, sizeof(value));
LE_TO_CPU_INT16(value, value);
return true;
}
@ -61,8 +65,9 @@ bool readUshort(std::istream& in, std::uint16_t& value)
bool readFloat(std::istream& in, float& value)
{
in.read(reinterpret_cast<char*>(&value), sizeof(float));
if (!in.good()) { return false; }
char buffer[sizeof(value)];
if (!in.read(buffer, sizeof(buffer)).good()) { return false; }
std::memcpy(&value, buffer, sizeof(value));
LE_TO_CPU_FLOAT(value, value);
return true;
}

View File

@ -18,6 +18,7 @@
#include <cassert>
#include <cmath>
#include <cstdio>
#include <memory>
using namespace cmod;
using namespace std;
@ -41,7 +42,7 @@ int main(int argc, char* argv[])
string inputFileName = argv[1];
cerr << "Reading...\n";
M3DScene* scene = Read3DSFile(inputFileName);
std::unique_ptr<M3DScene> scene = Read3DSFile(inputFileName);
if (scene == nullptr)
{
cerr << "Error reading 3DS file '" << inputFileName << "'\n";

View File

@ -8,6 +8,8 @@
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <fstream>
#include <memory>
#include "mainwindow.h"
#include "materialwidget.h"
#include "convert3ds.h"
@ -382,7 +384,7 @@ MainWindow::openModel(const QString& fileName)
if (info.suffix().toLower() == "3ds")
{
M3DScene* scene = Read3DSFile(fileNameStd);
std::unique_ptr<M3DScene> scene = Read3DSFile(fileNameStd);
if (scene == nullptr)
{
QMessageBox::warning(this, "Load error", tr("Error reading 3DS file %1").arg(fileName));
@ -396,8 +398,6 @@ MainWindow::openModel(const QString& fileName)
return;
}
delete scene;
// Generate normals for the model
double smoothAngle = 45.0; // degrees
double weldTolerance = 1.0e-6;