Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-79458

QUrl fromUserInput creates invalid urls

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4: Low
    • None
    • 5.13.0
    • Core: URL Handling
    • None

    Description

      QUrl has a different behavior when using the constructor vs. fromUserInput(). When trying to create a QUrl with the following invalid url :///home/hag/Pictures/below.png the constructor creates an empty QUrl and throws an error "Relative URL's path component contains ':' before any '/'; source was \":///home/hag/Pictures/below.png\"; path = \":///home/hag/Pictures/below.png\"", but the fromUserInput() function accepts the same invalid url without any errors and creates a local version of the url resulting in the following url file::///home/hag/Pictures/below.png.
      The following snippet should demonstrate the problem:

      QString str = QString(":///home/hag/Pictures/below.png");
      
      QUrl defaultUrl = QUrl(str);
      QUrl userUrl = QUrl::fromUserInput(str);
      
      qDebug() << "raw" << str;
      qDebug() << "default" << defaultUrl;
      qDebug() << "user" << userUrl;
      
      qDebug() << "ERROR default" << defaultUrl.errorString();
      qDebug() << "ERROR user" << userUrl.errorString();
      

      Output

      raw ":///home/hag/Pictures/below.png"
      default QUrl("")
      user QUrl("file::///home/hag/Pictures/below.png")
      ERROR default "Relative URL's path component contains ':' before any '/'; source was \":///home/hag/Pictures/below.png\"; path = \":///home/hag/Pictures/below.png\""
      ERROR user ""
      

      The fromUserInput() function needs a more sophisticated check than

      ...
      // Check first for files, since on Windows drive letters can be interpretted as schemes
      if (QDir::isAbsolutePath(trimmedString))
          return QUrl::fromLocalFile(trimmedString);
      ...
      

      This only checks if the given url is an absolute path which basically checks if it is a relative path and then negates the result. So whenever the provided url in fromUserInput() is not a relative path, this function will return the given url with the additional prefix file:.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            henning Henning Gründl
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes