From mboxrd@z Thu Jan 1 00:00:00 1970 From: Maxim Cournoyer Subject: bug#30116: [PATCH] `substitute' crashes when file contains NUL characters (core-updates)) Date: Sun, 14 Jan 2018 20:38:22 -0500 Message-ID: <87k1wjc35d.fsf_-_@gmail.com> References: <87r2qrc3mq.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/x-patch; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:40532) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eatjt-00074a-Sm for bug-guix@gnu.org; Sun, 14 Jan 2018 20:39:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eatjq-0001JI-PX for bug-guix@gnu.org; Sun, 14 Jan 2018 20:39:05 -0500 Received: from debbugs.gnu.org ([208.118.235.43]:48619) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eatjq-0001J1-Jt for bug-guix@gnu.org; Sun, 14 Jan 2018 20:39:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eatjq-0000vP-Dx for bug-guix@gnu.org; Sun, 14 Jan 2018 20:39:02 -0500 Sender: "Debbugs-submit" Resent-Message-ID: In-Reply-To: (GNU bug Tracking System's message of "Mon, 15 Jan 2018 01:29:02 +0000") Content-Disposition: attachment; filename=0001-utils-Prevent-substitute-from-crashing-on-files-cont.patch List-Id: Bug reports for GNU Guix List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guix-bounces+gcggb-bug-guix=m.gmane.org@gnu.org Sender: "bug-Guix" To: 30116@debbugs.gnu.org >From 9891e428eae0ed24e0d61862b3f5e298606b79eb Mon Sep 17 00:00:00 2001 From: Maxim Cournoyer Date: Sun, 14 Jan 2018 20:31:33 -0500 Subject: [PATCH] utils: Prevent substitute from crashing on files containing NUL chars. Fixes issue #30116. * guix/build/utils.scm (substitute): Add condition to skip lines containing the NUL character. --- guix/build/utils.scm | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/guix/build/utils.scm b/guix/build/utils.scm index 7391307c8..975f4e70a 100644 --- a/guix/build/utils.scm +++ b/guix/build/utils.scm @@ -3,6 +3,7 @@ ;;; Copyright =C2=A9 2013 Andreas Enge ;;; Copyright =C2=A9 2013 Nikita Karetnikov ;;; Copyright =C2=A9 2015 Mark H Weaver +;;; Copyright =C2=A9 2018 Maxim Cournoyer ;;; ;;; This file is part of GNU Guix. ;;; @@ -621,28 +622,35 @@ PROC as (PROC LINE MATCHES); PROC must return the lin= e that will be written as a substitution of the original line. Be careful about using '$' to match = the end of a line; by itself it won't match the terminating newline of a line." (let ((rx+proc (map (match-lambda - (((? regexp? pattern) . proc) - (cons pattern proc)) - ((pattern . proc) - (cons (make-regexp pattern regexp/extended) - proc))) + (((? regexp? pattern) . proc) + (cons pattern proc)) + ((pattern . proc) + (cons (make-regexp pattern regexp/extended) + proc))) pattern+procs))) (with-atomic-file-replacement file (lambda (in out) (let loop ((line (read-line in 'concat))) - (if (eof-object? line) - #t - (let ((line (fold (lambda (r+p line) - (match r+p - ((regexp . proc) - (match (list-matches regexp line) - ((and m+ (_ _ ...)) - (proc line m+)) - (_ line))))) - line - rx+proc))) - (display line out) - (loop (read-line in 'concat))))))))) + (cond + ((eof-object? line) + #t) + ((string-contains line (make-string 1 #\nul)) + ;; The regexp functions of the GNU C library (which Guile uses) + ;; cannot deal with NUL characters, so skip to the next line. + (format #t "skipping line with NUL characters: ~s\n" line) + (loop (read-line in 'concat))) + (else + (let ((line (fold (lambda (r+p line) + (match r+p + ((regexp . proc) + (match (list-matches regexp line) + ((and m+ (_ _ ...)) + (proc line m+)) + (_ line))))) + line + rx+proc))) + (display line out) + (loop (read-line in 'concat)))))))))) =20 =20 (define-syntax let-matches --=20 2.15.1