Summary: | [patch] Crash in KDirModelPrivate::nodeForUrl() if path contains two or more consecutive slashes | ||
---|---|---|---|
Product: | [Unmaintained] kio | Reporter: | Jonathan Thomas <echidnaman> |
Component: | general | Assignee: | David Faure <faure> |
Status: | RESOLVED FIXED | ||
Severity: | crash | CC: | frank78ac |
Priority: | NOR | ||
Version: | unspecified | ||
Target Milestone: | --- | ||
Platform: | unspecified | ||
OS: | Linux | ||
Latest Commit: | Version Fixed In: | ||
Sentry Crash Report: | |||
Attachments: |
Patch (including unit test)
updated patch |
Description
Jonathan Thomas
2008-10-30 21:27:12 UTC
Confirmed in trunk rev. 878290. The problem is that KDirModelPrivate::nodeForUrl() does not handle paths with two or more consecutive slashes properly. The patch below fixes it for me. Maybe I should also write a simple unit test to make sure that there is no regression in the future... Index: kio/kio/kdirmodel.cpp =================================================================== --- kio/kio/kdirmodel.cpp (revision 878290) +++ kio/kio/kdirmodel.cpp (working copy) @@ -186,7 +186,7 @@ if (url.protocol() != nodeUrl.protocol()) return 0; - const QString pathStr = url.path(); // no trailing slash + const QString pathStr = url.path().replace(QRegExp("//+"), "/"); // pathStr has no trailing slash, but multiple slashes must be removed KDirModelDirNode* dirNode = m_rootNode; if (!pathStr.startsWith(nodeUrl.path())) { @@ -203,7 +203,7 @@ // E.g. pathStr is /a/b/c and nodePath is /a. We want to find the child "b" in dirNode. const QString relativePath = pathStr.mid(nodePath.length()); - Q_ASSERT(!relativePath.startsWith('/')); // huh? we need double-slash simplification? + Q_ASSERT(!relativePath.startsWith('/')); // check if multiple slashes have been removed (see above) const int nextSlash = relativePath.indexOf('/'); const QString fileName = relativePath.left(nextSlash); // works even if nextSlash==-1 KDirModelNode* node = dirNode->m_childNodesByName.value(fileName); Created attachment 28261 [details]
Patch (including unit test)
Trying to write a unit test was a good idea - it turned out that additional work is needed to avoid that another assert is hit in some cases. The problem was that QUrl::operator == () does not consider "/a/b" and "/a//b" to be equal. This means that the comparison
if (nodeUrl == url)
failed, and instead of returning the node that was finally found, the following assert Q_ASSERT(isDir(node)) was hit.
David, can there actually be URLs where replacing multiple slashes by a single slash makes a difference, or should we report this to the Qt people?
Thanks for the patch. However QRegExp is awfully slow, I don't want it in KDirModel :-) How about KUrl::cleanPath()? Created attachment 28334 [details]
updated patch
David, you're right, KUrl::cleanPath() looks certainly better, and it also ensures that KDirModelPrivate::nodeForUrl() works properly for paths that contain "/../". I'll try to remember to have a look at the API docs of the involved classes the next time before I come up with a hack of my own ;-)
I've changed the patch accordingly. It's a one-liner now (except for the suggested unit test).
SVN commit 880241 by dfaure: Apply patch by Frank Reininghaus to fix "Crash in KDirModelPrivate::nodeForUrl() if path contains two or more consecutive slashes" BUG: 173927 Will backport to 4.1 branch. M +2 -1 kio/kdirmodel.cpp M +14 -0 tests/kdirmodeltest.cpp M +1 -0 tests/kdirmodeltest.h WebSVN link: http://websvn.kde.org/?view=rev&revision=880241 |