Tcl Library Source Code

View Ticket
Login
2026-11-16
00:06 • Ticket [187a6fa7ae] DNS client not handling partial TCP responses correctly status still Open with 3 other changes artifact: f7e75785c2 user: serhiy.storchaka
00:06 • Ticket [022bca0ff5] Fix for dns: can't read "state(size)": no such element in array status still Open with 3 other changes artifact: ee1295c5a1 user: serhiy.storchaka
2026-11-15
23:36
Fix [187a6fa7ae], [022bca0ff5]: the dns client did not handle TCP replies that arrive in several reads. Buffer until the 2-byte length has arrived, read the length unsigned, and take the id from the reply. leaf check-in: 79c8191d74 user: serhiy.storchaka tags: dns-tcp-partial-reads
2015-11-16
23:14 • New ticket [187a6fa7ae] DNS client not handling partial TCP responses correctly. artifact: 49e47b5a87 user: anonymous

Ticket UUID: 187a6fa7ae129ab84ae4819df9790929c849f3b1
Title: DNS client not handling partial TCP responses correctly
Type: Patch Version: 1.17
Submitter: anonymous Created on: 2015-11-16 23:14:34
Subsystem: dns Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2026-11-16 00:06:14
Resolution: None Closed By: nobody
    Closed on:
Description:
The proc ::dns::TcpEvent assumes that it has received at least 2 bytes for the response size when a readable event is triggered. In my particular case 1 byte arrived which triggered a read event. In this case the state(size) was not correctly populated and caused an exception. The patch below corrects this issue.


--- /tmp/SBo/tcllib-1.17/modules/dns/dns.tcl	2015-04-15 17:13:52.000000000 -0400
+++ dns.tcl	2015-11-16 12:31:57.000000000 -0500
@@ -828,31 +828,37 @@
         ${log}::debug "Event error: $result"
         Finish $token "error reading data: $result"
     } elseif { [string length $result] >= 0 } {
-        if {[catch {
-            # Handle incomplete reads - check the size and keep reading.
-            if {![info exists state(size)]} {
-                binary scan $result S state(size)
-                set result [string range $result 2 end]            
-            }
-            append state(reply) $result
-            
-            # check the length and flags and chop off the tcp length prefix.
-            if {[string length $state(reply)] >= $state(size)} {
-                binary scan $result S id
-                set id [expr {$id & 0xFFFF}]
-                if {$id != [expr {$state(id) & 0xFFFF}]} {
-                    ${log}::error "received packed with incorrect id"
-                }
-                # bug #1158037 - doing this causes problems > 65535 requests!
-                #Receive [namespace current]::$id
-                Receive $token
+
+        upvar 0 state(reply) reply
+        append reply $result
+
+        if {![info exists state(size)]} {
+            if {[string length $reply] >= 2} {
+                binary scan $reply S state(size)
+                set reply [string range $reply 2 end]
             } else {
-                ${log}::debug "Incomplete tcp read:\
-                   [string length $state(reply)] should be $state(size)"
+                # wait for more data
+                return
             }
-        } err]} {
-            Finish $token "Event error: $err"
         }
+        
+        if {[string length $reply] < $state(size)} {
+            # wait for more data
+            return
+        }
+
+        if {[binary scan $reply S id] != 1} {
+            Finish $token "Event error: reply too short"
+            return
+        }
+
+        if {[expr {($id & 0xffff) != ($state(id) & 0xffff)}]} {
+            Finish $token "Event error: mismatched reply id"
+            return
+        }
+
+        Receive $token
+
     } elseif { [eof $state(sock)] } {
         Eof $token
     } elseif { [fblocked $state(sock)] } {
User Comments: serhiy.storchaka added on 2026-11-16 00:06:14:

Proposed fix in branch dns-tcp-partial-reads, along the lines of the attached patch: buffer until the 2-byte length has arrived, read the length unsigned, and take the id from the reply. Test dns-4.1 uses a local server that splits the reply. [022bca0ff5] reports the same bug.