Skip to content

Commit 9a3a810

Browse files
committed
fix: 程序优化和健壮性增强;
1 parent 16bbf23 commit 9a3a810

42 files changed

Lines changed: 1744 additions & 1233 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3rdparty/QHexView/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ set_target_properties(
7575
CXX_STANDARD 17
7676
CXX_STANDARD_REQUIRED ON)
7777

78-
target_compile_definitions(QHexView PUBLIC QHEXVIEW_FIND_LIMIT=1000)
78+
target_compile_definitions(
79+
QHexView PUBLIC QHEXVIEW_FIND_LIMIT=1000 QHEXVIEW_BOOKMARK_LIMIT=100
80+
QHEXVIEW_METADATA_LIMIT=800 QHEXVIEW_COMMENT_LIMIT=100)
7981

8082
target_link_libraries(
8183
QHexView PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Gui

3rdparty/QHexView/QHexEdit2/chunks.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ QByteArray Chunks::data(qint64 pos, qint64 maxSize) const {
111111
byteCount = chunk.absPos - pos;
112112

113113
maxSize -= byteCount;
114-
_ioDevice->seek(pos + ioDelta);
114+
if (!_ioDevice->seek(pos + ioDelta)) {
115+
return {};
116+
}
115117
readBuffer = _ioDevice->read(byteCount);
116118
buffer += readBuffer;
117119
pos += readBuffer.size();
@@ -189,10 +191,16 @@ bool Chunks::insert(qint64 pos, const QByteArray &ba) {
189191

190192
auto length = ba.length();
191193
qsizetype chunkIdx;
192-
if (pos == _size)
194+
if (pos == _size) {
193195
chunkIdx = getChunkIndex(pos - 1);
194-
else
196+
} else {
195197
chunkIdx = getChunkIndex(pos);
198+
}
199+
200+
if (chunkIdx < 0) {
201+
return false;
202+
}
203+
196204
qint64 posInBa = pos - _chunks[chunkIdx].absPos;
197205
_chunks[chunkIdx].data.insert(posInBa, ba);
198206
for (auto idx = chunkIdx + 1; idx < _chunks.size(); idx++)
@@ -206,6 +214,9 @@ bool Chunks::overwrite(qint64 pos, const QByteArray &ba) {
206214
return false;
207215

208216
qsizetype chunkIdx = getChunkIndex(pos);
217+
if (chunkIdx < 0) {
218+
return false;
219+
}
209220

210221
auto &chunk = _chunks[chunkIdx];
211222
qint64 posInBa = pos - chunk.absPos;
@@ -232,6 +243,9 @@ bool Chunks::remove(qint64 pos, qint64 length) {
232243
return false;
233244

234245
qsizetype chunkIdx = getChunkIndex(pos);
246+
if (chunkIdx < 0) {
247+
return false;
248+
}
235249

236250
auto &chunk = _chunks[chunkIdx];
237251
qint64 posInBa = pos - chunk.absPos;
@@ -297,7 +311,9 @@ qsizetype Chunks::getChunkIndex(qint64 absPos) const {
297311
Chunk newChunk;
298312
qint64 readAbsPos = absPos - ioDelta;
299313
qint64 readPos = (readAbsPos & READ_CHUNK_MASK);
300-
_ioDevice->seek(readPos);
314+
if (!_ioDevice->seek(readPos)) {
315+
return -1;
316+
}
301317
newChunk.data = _ioDevice->read(CHUNK_SIZE);
302318
newChunk.absPos = absPos - (readAbsPos - readPos);
303319
_chunks.insert(insertIdx, newChunk);

3rdparty/QHexView/document/commands/bookmark/bookmarkaddcommand.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ inline QString constructText(qsizetype offset) {
2727
}
2828

2929
BookMarkAddCommand::BookMarkAddCommand(QHexDocument *doc, qsizetype pos,
30-
QString comment, QUndoCommand *parent)
31-
: BookMarkCommand(constructText(pos), doc, pos, comment, parent) {}
30+
const QString &comment,
31+
QUndoCommand *parent)
32+
: BookMarkCommand(constructText(pos), doc, pos, comment, parent) {
33+
if (m_doc->bookMarksCount() >= QHEXVIEW_BOOKMARK_LIMIT) {
34+
setObsolete(true);
35+
}
36+
}
3237

33-
void BookMarkAddCommand::redo() { m_doc->addBookMark(m_pos, m_comment); }
38+
void BookMarkAddCommand::redo() {
39+
if (!m_doc->addBookMark(m_pos, m_comment)) {
40+
setObsolete(true);
41+
}
42+
}
3443

3544
int BookMarkAddCommand::id() const { return UndoID_BookMarkAdd; }
3645

3rdparty/QHexView/document/commands/bookmark/bookmarkaddcommand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class BookMarkAddCommand : public BookMarkCommand {
2828
public:
2929
explicit BookMarkAddCommand(QHexDocument *doc, qsizetype pos,
30-
QString comment,
30+
const QString &comment,
3131
QUndoCommand *parent = nullptr);
3232

3333
// QUndoCommand interface

3rdparty/QHexView/document/commands/bookmark/bookmarkcommand.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "bookmarkcommand.h"
2323

2424
BookMarkCommand::BookMarkCommand(const QString &text, QHexDocument *doc,
25-
qsizetype pos, QString comment,
25+
qsizetype pos, const QString &comment,
2626
QUndoCommand *parent)
27-
: UndoCommandBase(text, parent), m_doc(doc), m_comment(comment),
28-
m_pos(pos) {}
27+
: UndoCommandBase(text, parent), m_doc(doc),
28+
m_comment(comment.left(QHEXVIEW_COMMENT_LIMIT)), m_pos(pos) {}

3rdparty/QHexView/document/commands/bookmark/bookmarkcommand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class BookMarkCommand : public UndoCommandBase {
2929
public:
3030
explicit BookMarkCommand(const QString &text, QHexDocument *doc,
31-
qsizetype pos, QString comment,
31+
qsizetype pos, const QString &comment,
3232
QUndoCommand *parent = nullptr);
3333

3434
protected:

3rdparty/QHexView/document/commands/bookmark/bookmarkremovecommand.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ inline QString constructText(qsizetype offset) {
2727
}
2828

2929
BookMarkRemoveCommand::BookMarkRemoveCommand(QHexDocument *doc, qsizetype pos,
30-
QString comment,
30+
const QString &comment,
3131
QUndoCommand *parent)
3232
: BookMarkCommand(constructText(pos), doc, pos, comment, parent) {}
3333

34-
void BookMarkRemoveCommand::redo() { m_doc->removeBookMark(m_pos); }
34+
void BookMarkRemoveCommand::redo() {
35+
if (!m_doc->removeBookMark(m_pos)) {
36+
setObsolete(true);
37+
}
38+
}
3539

3640
int BookMarkRemoveCommand::id() const { return UndoID_BookMarkRemove; }
3741

3rdparty/QHexView/document/commands/bookmark/bookmarkremovecommand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class BookMarkRemoveCommand : public BookMarkCommand {
2828
public:
2929
explicit BookMarkRemoveCommand(QHexDocument *doc, qsizetype pos,
30-
QString comment,
30+
const QString &comment,
3131
QUndoCommand *parent = nullptr);
3232

3333
// QUndoCommand interface

3rdparty/QHexView/document/commands/bookmark/bookmarkreplacecommand.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ inline QString constructText(qsizetype offset) {
2727
}
2828

2929
BookMarkReplaceCommand::BookMarkReplaceCommand(QHexDocument *doc, qsizetype pos,
30-
QString comment,
31-
QString oldcomment,
30+
const QString &comment,
31+
const QString &oldcomment,
3232
QUndoCommand *parent)
3333
: BookMarkCommand(constructText(pos), doc, pos, comment, parent),
3434
m_oldcomment(oldcomment) {}
3535

36-
void BookMarkReplaceCommand::redo() { m_doc->modBookMark(m_pos, m_comment); }
36+
void BookMarkReplaceCommand::redo() {
37+
if (!m_doc->modBookMark(m_pos, m_comment)) {
38+
setObsolete(true);
39+
}
40+
}
3741

3842
int BookMarkReplaceCommand::id() const { return UndoID_BookMarkReplace; }
3943

3rdparty/QHexView/document/commands/bookmark/bookmarkreplacecommand.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
class BookMarkReplaceCommand : public BookMarkCommand {
2828
public:
2929
explicit BookMarkReplaceCommand(QHexDocument *doc, qsizetype pos,
30-
QString comment, QString oldcomment,
30+
const QString &comment,
31+
const QString &oldcomment,
3132
QUndoCommand *parent = nullptr);
3233

3334
// QUndoCommand interface

0 commit comments

Comments
 (0)