Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stardict qstring modification #1708

Merged
merged 9 commits into from
Jul 25, 2024
Merged

fix: stardict qstring modification #1708

merged 9 commits into from
Jul 25, 2024

Conversation

xiaoyifang
Copy link
Owner

@xiaoyifang xiaoyifang commented Jul 25, 2024

the stardict qstring part has caused crash sometimes.
Fix #1702

basic rule: avoid unnecessary conversions between QString and string

Changes:
1.

QString newTag   = QString::fromUtf8(
            ( addAudioLink( href, getId() ) + "<span class=\"sdict_h_wav\"><a href=" + href + ">" ).c_str() );

to
string only.

  1. return (articleText.toUtf8.data());
    to
     auto text = articleText.toUtf8();
      return text.data();

QString stripHtml( const QString & tmp )
to
QString stripHtml( QString tmp )

@xiaoyifang xiaoyifang changed the title Fix/stardict qstring fix: stardict qstring modification Jul 25, 2024
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarCloud


return articleText.toStdString();
auto text = articleText.toUtf8();
return text.data();
}
Copy link
Collaborator

@shenlebantongying shenlebantongying Jul 25, 2024

Choose a reason for hiding this comment

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

The difference is pretty subtle.

  • in the new code, std::string(toUtf8().data()) will seek for the null byte again
  • in the old code, toStdString's std::string(toUtf8().data(),toUtf8().size()) will use the articleText's actual length

Maybe there should be a comment that clarify articleText may contain null byte, and we want to cut off earlier?

Copy link
Owner Author

@xiaoyifang xiaoyifang Jul 25, 2024

Choose a reason for hiding this comment

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

the original code is
return (articleText.toUtf8().data()); I changed it into return articleText.toStdString() ,crash still reported on this line.
So I split it into two lines.
I guess articleText.toUtf8() will generate a temporary string . when chained with .data() . sometimes the temporary variable has no longer existed. and a crash will throw.

From real environment test ,the crash has more probability to happen when in multithread cases.

Copy link
Owner Author

Choose a reason for hiding this comment

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

  • in the old code, toStdString's std::string(toUtf8().data(),toUtf8().size()) will use the articleText's actual length

the old code is return (articleText.toUtf8.data()); ,toStdString() is my first attempt.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

Copy link
Owner Author

@xiaoyifang xiaoyifang Jul 25, 2024

Choose a reason for hiding this comment

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

I also can not be sure.

But Qt+MSVC +QString are more likely to have issue as the https://bugreports.qt.io/browse/QTBUG-63274 implies

Maybe some link option in CMAKE , /MD /MT etc?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems to be the correct way to set /MD /MT

set_property(TARGET foo PROPERTY
  MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")

https://github.com/qt/qtbase/blob/02cb165ef8050230b477358e4136e9f0acd83eb6/cmake/QtPublicTargetHelpers.cmake#L395-L397

However, CMake's doc says

If the property is not set, then CMake uses the default value MultiThreaded$<$CONFIG:Debug:Debug>DLL to select a MSVC runtime library.

So, I suppose it is set correctly by CMake already.

https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html

Copy link
Owner Author

@xiaoyifang xiaoyifang Jul 25, 2024

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

No,cmake_minimum_required will set policy version, and we currently set it aggressively high 😅.

https://cmake.org/cmake/help/latest/command/cmake_minimum_required.html#policy-settings

cmake_minimum_required(VERSION 3.25) # ubuntu 23.04 Fedora 36

Copy link
Collaborator

@shenlebantongying shenlebantongying Jul 25, 2024

Choose a reason for hiding this comment

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

I think searching in the build.ninja file in the build folder can confirm if /MD /MT is set.

On my machine, /MDd is automatically set.

@xiaoyifang
Copy link
Owner Author

xiaoyifang commented Jul 25, 2024

Good news is that ,with the fix, the crash has gone. really mysterious.

@shenlebantongying
Copy link
Collaborator

The changes look fine. For merging this, I don't think we need to know the root cause, which may reveal in the future.

Old & new code should be equivalent if things work as they are documented/written anyway 😅.

@xiaoyifang xiaoyifang merged commit e2617f3 into staged Jul 25, 2024
12 of 13 checks passed
@xiaoyifang xiaoyifang deleted the fix/stardict-qstring branch July 25, 2024 08:45
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.

crash when make full-text index on certain stardict dictionaries.
2 participants