Sockets: Fix potential null pointer usages
authorShane Kearns <shane.kearns@accenture.com>
Thu, 5 May 2011 15:32:11 +0000 (16:32 +0100)
committerOlivier Goffart <olivier.goffart@nokia.com>
Wed, 11 May 2011 14:39:40 +0000 (16:39 +0200)
QAbstractSocketEngine::createSocketEngine can return 0 as well as throw.
In two cases the pointer was being used before the null check, in a 3rd
case the null check was missing.

Reviewed-by: Markus Goetz
(cherry picked from commit 19edac88af53eea7f733cabbaee77f9b725b7ea9)

src/network/socket/qabstractsocket.cpp
src/network/socket/qtcpserver.cpp

index d8d2633..fc0bb85 100644 (file)
@@ -547,15 +547,15 @@ bool QAbstractSocketPrivate::initSocketLayer(QAbstractSocket::NetworkLayerProtoc
 
     resetSocketLayer();
     socketEngine = QAbstractSocketEngine::createSocketEngine(q->socketType(), proxyInUse, q);
-#ifndef QT_NO_BEARERMANAGEMENT
-    //copy network session down to the socket engine (if it has been set)
-    socketEngine->setProperty("_q_networksession", q->property("_q_networksession"));
-#endif
     if (!socketEngine) {
         socketError = QAbstractSocket::UnsupportedSocketOperationError;
         q->setErrorString(QAbstractSocket::tr("Operation on socket is not supported"));
         return false;
     }
+#ifndef QT_NO_BEARERMANAGEMENT
+    //copy network session down to the socket engine (if it has been set)
+    socketEngine->setProperty("_q_networksession", q->property("_q_networksession"));
+#endif
 #ifndef QT_NO_NETWORKPROXY
     //copy user agent to socket engine (if it has been set)
     socketEngine->setProperty("_q_user-agent", q->property("_q_user-agent"));
@@ -1609,15 +1609,15 @@ bool QAbstractSocket::setSocketDescriptor(int socketDescriptor, SocketState sock
 
     d->resetSocketLayer();
     d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this);
-#ifndef QT_NO_BEARERMANAGEMENT
-    //copy network session down to the socket engine (if it has been set)
-    d->socketEngine->setProperty("_q_networksession", property("_q_networksession"));
-#endif
     if (!d->socketEngine) {
         d->socketError = UnsupportedSocketOperationError;
         setErrorString(tr("Operation on socket is not supported"));
         return false;
     }
+#ifndef QT_NO_BEARERMANAGEMENT
+    //copy network session down to the socket engine (if it has been set)
+    d->socketEngine->setProperty("_q_networksession", property("_q_networksession"));
+#endif
     bool result = d->socketEngine->initialize(socketDescriptor, socketState);
     if (!result) {
         d->socketError = d->socketEngine->error();
index 5a60764..026ceb4 100644 (file)
@@ -416,6 +416,11 @@ bool QTcpServer::setSocketDescriptor(int socketDescriptor)
     if (d->socketEngine)
         delete d->socketEngine;
     d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this);
+    if (!d->socketEngine) {
+        d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError;
+        d->serverSocketErrorString = tr("Operation on socket is not supported");
+        return false;
+    }
 #ifndef QT_NO_BEARERMANAGEMENT
     //copy network session down to the socket engine (if it has been set)
     d->socketEngine->setProperty("_q_networksession", property("_q_networksession"));