Skip to content

Commit fc7fffe

Browse files
committed
Fix memory corruption (marker was getting overflowed because
some strings are 8 characters. With the null chararacter, it needed to be 9 chars. It was only 8. Also, fixed the non rw option.
1 parent 17a7a68 commit fc7fffe

File tree

4 files changed

+49
-72
lines changed

4 files changed

+49
-72
lines changed

src/app.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void CoderApp::GenerateCode()
3535
CiMainGenerator cigen;
3636
CiUtilGenerator ciugen;
3737
FsCreator fscreator;
38+
std::string path;
3839

3940
std::ifstream reader;
4041

@@ -57,9 +58,18 @@ void CoderApp::GenerateCode()
5758
std::string info("");
5859

5960
// create main destination directory
60-
fscreator.Configure(Params.drvname.first, Params.outdir.first, info, scanner.dblist.ver.hi, scanner.dblist.ver.low);
61+
if (!Params.is_rewrite)
62+
{
63+
path = fscreator.FindPath(Params.outdir.first);
64+
}
65+
else
66+
{
67+
path = Params.outdir.first;
68+
}
69+
70+
fscreator.Configure(Params.drvname.first, path, info, scanner.dblist.ver.hi, scanner.dblist.ver.low);
6171

62-
auto ret = fscreator.PrepareDirectory(Params.is_rewrite);
72+
auto ret = fscreator.PrepareDirectory();
6373

6474
fscreator.FS.gen.no_config = Params.is_noconfig;
6575
fscreator.FS.gen.no_inc = Params.is_nocanmon;

src/codegen/fs-creator.cpp

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <sys/stat.h>
2+
#include <iostream>
23
#include <filesystem>
34
#include "fs-creator.h"
45
#include "helpers/formatter.h"
@@ -17,6 +18,23 @@ FsCreator::FsCreator()
1718
{
1819
}
1920

21+
std::string FsCreator::FindPath(const std::string& outpath)
22+
{
23+
std::string work_dir_path;
24+
std::string separator = work_dir_path.back() == '/' ? "" : "/";
25+
26+
for (uint32_t dirnum = 0; dirnum < 1000; dirnum++)
27+
{
28+
snprintf(_tmpb, kTmpLen, "%03d", dirnum);
29+
work_dir_path = outpath + separator + _tmpb;
30+
31+
if (!std::filesystem::exists(work_dir_path))
32+
{
33+
break;
34+
}
35+
}
36+
return work_dir_path;
37+
}
2038

2139
void FsCreator::Configure(const std::string& drvname, const std::string& outpath,
2240
const std::string& info, uint32_t h, uint32_t l)
@@ -94,61 +112,18 @@ void FsCreator::Configure(const std::string& drvname, const std::string& outpath
94112
FS.gen.lowver = l;
95113
}
96114

97-
bool FsCreator::PrepareDirectory(bool rw)
115+
bool FsCreator::PrepareDirectory()
98116
{
99117
bool ret = false;
100-
101-
// find free directory
102-
struct stat info;
103-
104-
std::string work_dir_path;
118+
std::error_code ec;
105119
const auto& basepath = FS.file.core_h.dir;
106120

107-
if (rw)
108-
{
109-
work_dir_path = basepath;
110-
ret = true;
111-
112-
// for this case check only if directory exists
113-
if (stat(work_dir_path.c_str(), &info) != 0)
114-
{
115-
if (!std::filesystem::create_directory(work_dir_path))
116-
{
117-
ret = false;
118-
}
119-
}
120-
}
121-
else
121+
std::filesystem::create_directories(basepath, ec);
122+
if (ec)
122123
{
123-
std::string separator = basepath.back() == '/' ? "" : "/";
124-
125-
for (int32_t dirnum = 0; dirnum < 1000; dirnum++)
126-
{
127-
snprintf(_tmpb, kTmpLen, "%03d", dirnum);
128-
work_dir_path = basepath + separator + _tmpb;
129-
130-
if (stat(work_dir_path.c_str(), &info) != 0)
131-
{
132-
if (std::filesystem::create_directory(work_dir_path))
133-
{
134-
ret = true;
135-
break;
136-
}
137-
}
138-
else if (info.st_mode & S_IFDIR)
139-
{
140-
// directory exists, try next num
141-
continue;
142-
}
143-
else
144-
{
145-
if (std::filesystem::create_directory(work_dir_path) != 0)
146-
{
147-
ret = false;
148-
break;
149-
}
150-
}
151-
}
124+
// If there is an error, ec will evaluate to true
125+
std::cerr << "Error creating directories: " << ec.message() << std::endl;
126+
return false;
152127
}
153128

154129
std::filesystem::create_directory(FS.file.libdir);
@@ -157,7 +132,7 @@ bool FsCreator::PrepareDirectory(bool rw)
157132
std::filesystem::create_directory(FS.file.confdir);
158133
std::filesystem::create_directory(FS.file.utildir);
159134

160-
return ret;
135+
return true;
161136
}
162137

163138
std::string FsCreator::CreateSubDir(std::string basepath, std::string sub, bool rw)

src/codegen/fs-creator.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,12 @@ typedef struct
6868
class FsCreator {
6969
public:
7070
FsCreator();
71-
71+
std::string FindPath(const std::string& outpath);
7272
void Configure(const std::string& drvname, const std::string& outpath, const std::string& info, uint32_t h, uint32_t l);
73-
bool PrepareDirectory(bool rw);
73+
bool PrepareDirectory();
7474

7575
std::string CreateSubDir(std::string basepath, std::string subdir, bool rm = true);
7676

7777
AppSettings_t FS;
7878

7979
};
80-

src/parser/dbcscanner.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "dbcscanner.h"
22
#include <cstring>
3+
#include <sstream>
34
#include <algorithm>
45
#include <math.h>
56
#include "../helpers/formatter.h"
@@ -341,23 +342,15 @@ void DbcScanner::SetDefualtMessage(MessageDescriptor_t* message)
341342
void DbcScanner::FindVersion(const std::string& instr)
342343
{
343344
// try to find version string which looks like: VERSION "x.x"
344-
static constexpr char* versionAttr = (char*)"VERSION";
345-
static constexpr size_t VER_MIN_LENGTH = strlen(versionAttr);
346-
345+
std::istringstream iss(instr);
346+
std::string marker;
347+
char token;
347348
uint32_t h = 0, l = 0;
348-
char marker[VER_MIN_LENGTH + 1u];
349-
350-
if (instr.size() < VER_MIN_LENGTH)
351-
{
352-
return;
353-
}
354349

355-
auto ret = std::sscanf(instr.c_str(), "%8s \"%u.%u\"", marker, &h, &l);
356-
357-
if ((ret == 3) && (std::strcmp(marker, versionAttr) == 0))
358-
{
359-
// versions have been found, save numeric values
360-
dblist.ver.hi = h;
361-
dblist.ver.low = l;
350+
if (iss >> marker >> token && token == '"' && marker == "VERSION") {
351+
if (iss >> h >> token && token == '.' && iss >> l) {
352+
dblist.ver.hi = h;
353+
dblist.ver.low = l;
354+
}
362355
}
363356
}

0 commit comments

Comments
 (0)