Skip to content

fix: Refactor file saving with TextFileSaver and add safety checks#348

Merged
rb-union merged 2 commits into
linuxdeepin:masterfrom
rb-union:master
Apr 21, 2025
Merged

fix: Refactor file saving with TextFileSaver and add safety checks#348
rb-union merged 2 commits into
linuxdeepin:masterfrom
rb-union:master

Conversation

@rb-union
Copy link
Copy Markdown
Contributor

@rb-union rb-union commented Apr 21, 2025

fix: Refactor file saving with TextFileSaver and add safety checks

  • Added TextFileSaver class for atomic file saving with encoding conversion
  • Refactored all file saving operations in EditWrapper to use TextFileSaver
  • Added null checks in Window class for safer wrapper access
  • Improved error handling for long filenames and permission issues
  • Removed duplicate encoding conversion code paths

Log: Improved file saving reliability and added safety checks
throughout the editor

fix: Refactor file saving with memory validation

  • Added memory checks before document processing
  • Implemented TextFileSaver class for safer handling
  • Optimized with chunked writes and progress events

Log: Improved file saving reliability with memory checks.

Summary by Sourcery

Refactor file saving mechanism in the text editor to improve reliability, safety, and memory management

New Features:

  • Introduced TextFileSaver class for atomic and safe file saving with advanced encoding conversion

Bug Fixes:

  • Fixed potential memory-related crashes during copy and paste operations
  • Added safety checks to prevent file saving with insufficient system memory
  • Resolved encoding conversion issues with fallback mechanisms

Enhancements:

  • Implemented chunked file writing to handle large documents
  • Added comprehensive memory checks before file operations
  • Improved error handling for file saving scenarios

Tests:

  • Added memory sufficiency checks for document operations
  • Implemented robust error handling in file saving process

- Added TextFileSaver class for atomic file saving with encoding conversion
- Refactored all file saving operations in EditWrapper to use TextFileSaver
- Added null checks in Window class for safer wrapper access
- Improved error handling for long filenames and permission issues
- Removed duplicate encoding conversion code paths

Log: Improved file saving reliability and added safety checks
     throughout the editor
- Added memory checks before document processing
- Implemented TextFileSaver class for safer handling
- Optimized with chunked writes and progress events

Log: Improved file saving reliability with memory checks.
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 21, 2025

Reviewer's Guide by Sourcery

This pull request introduces the TextFileSaver class to handle file saving with encoding conversion, refactors file saving operations in EditWrapper to use TextFileSaver, adds memory checks to prevent crashes due to large file operations, and implements safety checks to prevent null pointer dereferences. It also improves error handling for file saving operations.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Introduced TextFileSaver class for handling file saving operations.
  • Implemented atomic file saving.
  • Handled encoding conversions.
  • Added error handling for file operations.
src/common/text_file_saver.h
src/common/text_file_saver.cpp
src/editor/editwrapper.cpp
Refactored file saving logic in EditWrapper to use TextFileSaver.
  • Replaced direct file writing with TextFileSaver calls in saveAsFile and saveFile methods.
  • Simplified file saving code by delegating encoding and writing logic to TextFileSaver.
  • Removed redundant encoding conversion code.
src/editor/editwrapper.cpp
Added memory checks to prevent crashes due to large file operations.
  • Implemented getSystemMemoryInfo in Utils to retrieve system memory usage.
  • Implemented isMemorySufficientForOperation in Utils to check if enough memory is available before copy, paste, and save operations.
  • Added memory checks before document processing and encoding conversion in TextFileSaver.
  • Added memory checks before copy and paste operations in DTextEdit.
src/common/utils.h
src/common/utils.cpp
src/editor/dtextedit.cpp
src/common/text_file_saver.cpp
Implemented safety checks to prevent null pointer dereferences.
  • Added null checks in Window class for safer access to EditWrapper instances.
  • Added null checks in Window class for safer access to TextEdit instances.
src/widgets/window.cpp
Improved error handling for file saving operations.
  • Added checks for long filenames to avoid issues with temporary file creation.
  • Improved error messages for file permission issues.
  • Return error if converted data is empty.
src/editor/editwrapper.cpp
src/common/text_file_saver.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @rb-union - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a progress indicator or some form of user feedback during the file saving process, especially for large files.
  • The introduction of TextFileSaver seems like a good abstraction; ensure it's thoroughly tested, especially error conditions.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@github-actions
Copy link
Copy Markdown

  • 敏感词检查失败, 检测到1个文件存在敏感词
详情
{
    "src/widgets/window.cpp": [
        {
            "line": "    QString key = \"base/enable\";",
            "line_number": 372,
            "rule": "S106",
            "reason": "Var naming | 64f28539d9"
        }
    ]
}

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, rb-union

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@rb-union rb-union merged commit 7edacc5 into linuxdeepin:master Apr 21, 2025
18 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants