fix: UTF-8 guard in cmd-show, version consistency, byte-length accounting, decimal port validation, write-all negative check

ober

67b6e4b27bd4e9e1c1f6999b9a5378f1af3b6e49

diff --git a/protonmail/cli.ss b/protonmail/cli.ss
index 58088e3..0c3ba75 100644
--- a/protonmail/cli.ss
+++ b/protonmail/cli.ss
@@ -14,7 +14,7 @@
           (jerboa-mail header)
           (jerboa-mail mime))
 
-  (define version "0.0.0-phase0")
+  (define version "0.0.0-phase7")
 
   (define usage-string
     (string-append
@@ -219,7 +219,8 @@
 
   (define (cmd-show args)
     (let* ([raw (fetch-raw-from-args args)]
-           [raw-text (utf8->string raw)]
+           [raw-text (guard (e [#t (die 1 "error: message body is not valid UTF-8")])
+                       (utf8->string raw))]
            [msg (mail-parse-message raw-text)]
            [headers (mail-message-headers msg)])
       (print-header-if-present "From" (decoded-header headers "From"))
diff --git a/protonmail/config.ss b/protonmail/config.ss
index e2e56b2..2db5602 100644
--- a/protonmail/config.ss
+++ b/protonmail/config.ss
@@ -32,10 +32,12 @@
       (if (and value (not (string=? value ""))) value #f)))
 
   (define (valid-port-string->number s)
-    (let ([n (string->number s)])
-      (if (and n (integer? n) (> n 0) (< n 65536))
-          n
-          #f)))
+    (and (> (string-length s) 0)
+         (for-all (lambda (c) (char<=? #\0 c #\9)) (string->list s))
+         (let ([n (string->number s 10)])
+           (if (and n (integer? n) (> n 0) (< n 65536))
+               n
+               #f))))
 
   (define (config-from-environment)
     (let ([port-env (env "PROTON_BRIDGE_PORT")])
diff --git a/protonmail/imap/framing.ss b/protonmail/imap/framing.ss
index 11863a4..ee99e69 100644
--- a/protonmail/imap/framing.ss
+++ b/protonmail/imap/framing.ss
@@ -112,7 +112,7 @@
           (error 'imap-response "IMAP response entry count exceeds configured limit"))
         (let* ([line (checked-line read-line max-line deadline)]
                [line-total (checked-add-total total
-                                             (+ (string-length line) 2)
+                                             (+ (bytevector-length (string->utf8 line)) 2)
                                              max-total)]
                [response (imap-parse-line line)]
                [literal-length (imap-response-literal-length response)])
diff --git a/protonmail/secure-file.ss b/protonmail/secure-file.ss
index 94f75b5..6d21231 100644
--- a/protonmail/secure-file.ss
+++ b/protonmail/secure-file.ss
@@ -196,8 +196,8 @@
                                    (bytevector-copy! chunk sent rest 0 remaining)
                                    rest))]
                        [written (posix-write fd part remaining)])
-                  (when (= written 0)
-                    (error 'secure-export-bytes "zero-length file write"))
+                  (when (<= written 0)
+                    (error 'secure-export-bytes "file write failed" written))
                   (write-loop (+ sent written)))))
             (loop (+ offset count)))))))