shithub: mc

Download patch

ref: a9654273ae02c19160f5bf818b51036bfaba1f0c
parent: 55e5a374f0f22d66dbc6707a9aab1559248b81da
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Jan 19 19:59:50 EST 2016

Improve dial() and resolve()

	- Fix a couple of fd leaks
	- Guarantee that all bytes sent to the system are initialized.

--- a/lib/std/dial+posixy.myr
+++ b/lib/std/dial+posixy.myr
@@ -34,6 +34,7 @@
 	var sa4 : sys.sockaddr_in
 	var sa6 : sys.sockaddr_in6
 	var sa	: sys.sockaddr#
+	var a
 	var sock
 
 	match parsedial(str)
@@ -43,17 +44,21 @@
 
 	match getaddr(host)
 	| `Ipv4 bits:
-		sa4.fam = sys.Afinet
-		sa4.addr = bits
-		sa4.port = hosttonet(port)
+		sa4 = [
+			.fam = sys.Afinet,
+			.addr = bits,
+			.port = hosttonet(port),
+		]
 		sa = &sa4 castto(sys.sockaddr#)
 	| `Ipv6 bits:
-		sa6.fam = sys.Afinet6
-		sa6.addr = bits
-		sa6.port = hosttonet(port)
+		sa6 = [
+			.fam = sys.Afinet6,
+			.addr = bits,
+			.port = hosttonet(port),
+		]
 		sa = &sa6 castto(sys.sockaddr#)
 	;;
-	sock = sys.socket(sa.fam, proto, 0)
+	sock = sys.socket(2, proto, 0)
 
 	if sock < 0
 		-> `Fail "failed to connect to socket"
@@ -128,7 +133,8 @@
 	var ip
 
 	match ipparse(addr)
-	| `Some a:	ip = a
+	| `Some a:
+		ip = a
 	| `None:
 		match resolve(addr)
 		| `Ok hi:
--- a/lib/std/resolve+posixy.myr
+++ b/lib/std/resolve+posixy.myr
@@ -212,7 +212,7 @@
 
 
 const dnsresolve = {host, rt
-	var nsrv
+	var nsrv, r
 
 	if !valid(host)
 		-> `Fail (`Badhost)
@@ -220,7 +220,9 @@
 	for ns in nameservers
 		nsrv = dnsconnect(ns)
 		if nsrv >= 0
-			-> dnsquery(nsrv, host, rt)
+			r = dnsquery(nsrv, host, rt)
+			sys.close(nsrv)
+			-> r
 		;;
 	;;
 	-> `Fail (`Badsrv)
@@ -242,9 +244,11 @@
 	if s < 0
 		-> -1
 	;;
-	sa.fam = sys.Afinet
-	sa.port = hosttonet(53)
-	sa.addr = addr
+	sa = [
+		.fam = sys.Afinet,
+		.port = hosttonet(53),
+		.addr = addr,
+	]
 	status = sys.connect(s, (&sa) castto(sys.sockaddr#), sizeof(sys.sockaddr_in))
 	if status < 0
 		-> -1
--- a/lib/std/slurp.myr
+++ b/lib/std/slurp.myr
@@ -14,9 +14,13 @@
 const Bufstart = 4096
 
 const slurp = {path
+	var sl
 	match open(path, Ordonly)
-	| `Ok fd:	-> fslurp(fd)
 	| `Fail e:	-> `Fail e
+	| `Ok fd:
+		sl = fslurp(fd)
+		close(fd)
+		-> sl
 	;;
 }