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

QUdpSocket::readDatagram doesn't set sender host/port if receive buffer is too small

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3: Somewhat important
    • Resolution: Done
    • Affects Version/s: 5.9.4, 5.9.6, 5.9.8
    • Fix Version/s: 5.12.0
    • Component/s: Network: Sockets
    • Labels:
      None
    • Platform/s:
      Windows
    • Commits:
      6afd5990c79d86d4136b41018066c05c95d1b0d2

      Description

      After QUdpSocket::readyRead was triggered, the method QUdpSocket::pendingDatagramSize returns 1201 bytes (the whole UDP datagram i sent from the remote side). When calling QUdpSocket::readDatagram with only maxSize = 1200, the address and port parameters are not filled with the sender information (pointers are valid).

      A look into the Qt source code revealed the reason in the file qnativesocketengine_win.cpp in method QNativeSocketEnginePrivate::nativeReceiveDatagram():

      ...
      if (ret == SOCKET_ERROR) {
          int err = WSAGetLastError();
          if (err == WSAEMSGSIZE) {
              // it is ok the buffer was to small if bytesRead is larger than
              // maxLength then assume bytes read is really maxLenth
              ret = qint64(bytesRead) > maxLength ? maxLength : qint64(bytesRead);
          } else {
              WS_ERROR_DEBUG(err);
              switch (err) {
              case WSAENETRESET:
                  setError(QAbstractSocket::NetworkError, NetworkDroppedConnectionErrorString);
                  break;
              case WSAECONNRESET:
                  setError(QAbstractSocket::ConnectionRefusedError, ConnectionResetErrorString);
                  break;
              default:
                  setError(QAbstractSocket::NetworkError, ReceiveDatagramErrorString);
                  break;
              }
              ret = -1;
              if (header)
                  header->clear();
          }
      } else {
          ret = qint64(bytesRead);
          if (options & QNativeSocketEngine::WantDatagramSender)
              qt_socket_getPortAndAddress(socketDescriptor, &aa, &header->senderPort, &header->senderAddress);
      }
      ...

      The lines

      if (options & QNativeSocketEngine::WantDatagramSender)
          qt_socket_getPortAndAddress(socketDescriptor, &aa, &header->senderPort, &header->senderAddress);

      needs to be executed also right after

          if (err == WSAEMSGSIZE) {
              // it is ok the buffer was to small if bytesRead is larger than
              // maxLength then assume bytes read is really maxLenth
              ret = qint64(bytesRead) > maxLength ? maxLength : qint64(bytesRead);

      because this is a tolerated socket error.

        Attachments

          Issue Links

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

            Activity

              People

              • Assignee:
                thiago Thiago Macieira
                Reporter:
                coder315 Andre Stachowiak
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Gerrit Reviews

                  There are no open Gerrit changes