Merge branch 'topic/serverlink-fixes' of git://valera-ext.nynaeve.net/silc into silc...
[silc.git] / lib / silccore / silcpacket.c
index 2f491acb5f0997f2e5c30455be8a9eb638954eb9..9a44f87c9f74948fce9202b1fcfc43bbcf6fba14 100644 (file)
@@ -1108,21 +1108,19 @@ void silc_packet_stream_unlink(SilcPacketStream stream,
 
   silc_mutex_lock(stream->lock);
 
-  if (stream->process) {
-    silc_dlist_start(stream->process);
-    while ((p = silc_dlist_get(stream->process)) != SILC_LIST_END)
-      if (p->callbacks == callbacks &&
-         p->callback_context == callback_context) {
-        silc_dlist_del(stream->process, p);
-        silc_free(p->types);
-        silc_free(p);
-        break;
-      }
-
-    if (!silc_dlist_count(stream->process)) {
-      silc_dlist_uninit(stream->process);
-      stream->process = NULL;
+  silc_dlist_start(stream->process);
+  while ((p = silc_dlist_get(stream->process)) != SILC_LIST_END)
+    if (p->callbacks == callbacks &&
+       p->callback_context == callback_context) {
+      silc_dlist_del(stream->process, p);
+      silc_free(p->types);
+      silc_free(p);
+      break;
     }
+
+  if (!silc_dlist_count(stream->process)) {
+    silc_dlist_uninit(stream->process);
+    stream->process = NULL;
   }
 
   silc_mutex_unlock(stream->lock);
@@ -2199,10 +2197,10 @@ static void silc_packet_read_process(SilcPacketStream stream)
     /* Get packet length and full packet length with padding */
     SILC_PACKET_LENGTH(header, packetlen, paddedlen);
 
-    /* Sanity checks */
-    if (silc_unlikely(packetlen < SILC_PACKET_MIN_LEN)) {
-      if (!stream->udp && !silc_socket_stream_is_udp(stream->stream, NULL))
-       SILC_LOG_ERROR(("Received too short packet"));
+    /* Padding sanity checks */
+    if (cipher && (paddedlen % block_len) != 0) {
+      SILC_LOG_DEBUG(("Packet length %d not multiple by cipher block length",
+                     paddedlen));
       silc_mutex_unlock(stream->lock);
       SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MALFORMED);
       silc_mutex_lock(stream->lock);
@@ -2233,6 +2231,17 @@ static void silc_packet_read_process(SilcPacketStream stream)
       goto out;
     }
 
+    /* Sanity checks */
+    if (silc_unlikely(packetlen < SILC_PACKET_MIN_LEN)) {
+      if (!stream->udp && !silc_socket_stream_is_udp(stream->stream, NULL))
+       SILC_LOG_ERROR(("Received too short packet"));
+      silc_mutex_unlock(stream->lock);
+      SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MALFORMED);
+      silc_mutex_lock(stream->lock);
+      memset(tmp, 0, sizeof(tmp));
+      goto out;
+    }
+
     /* Get packet */
     packet = silc_packet_alloc(stream->sc->engine);
     if (silc_unlikely(!packet)) {