Raymond Toy pushed to branch issue-375-mkstemp-return-filename at cmucl / cmucl Commits: 3b6909fe by Raymond Toy at 2025-02-03T07:26:09-08:00 Enforce 6 X's in template for mkstemp/mkdtemp Our API enforces the template to have exactly 6 X's at the end of the template. This is only a problem on Darwin which allows 0 or more X's. Also re-enable the tests for too few X's on Darwin and add tests for too many X's. - - - - - 2759e80f by Raymond Toy at 2025-02-03T07:28:50-08:00 Add tests for template that doesn't end in X's. - - - - - 2 changed files: - src/code/unix.lisp - tests/unix.lisp Changes: ===================================== src/code/unix.lisp ===================================== @@ -2904,6 +2904,15 @@ (function (* char)))) c-string)) +(defun check-template (template) + ;; Make sure the template ends with exactly 6 X's and no more. + (let ((last-non-x (position-if-not #'(lambda (c) + (char= c #\X)) + template + :from-end t))) + (and last-non-x + (= last-non-x (- (length template) 7))))) + (defun unix-mkstemp (template) _N"Generates a unique temporary file name from TEMPLATE, and creates and opens the file. On success, the corresponding file descriptor @@ -2911,6 +2920,10 @@ code is returned. The last six characters of the template must be \"XXXXXX\"." + ;; Make sure the template is valid. + (unless (check-template template) + (return-from unix-mkstemp + (values nil einval))) (let* ((format (if (eql *filename-encoding* :null) :iso8859-1 *filename-encoding*)) @@ -2942,6 +2955,10 @@ If the directory cannot be created NIL and the UNIX error code is returned." + ;; Make sure the template is valid. + (unless (check-template template) + (return-from unix-mkstemp + (values nil einval))) (let* ((format (if (eql *filename-encoding* :null) :iso8859-1 *filename-encoding*)) ===================================== tests/unix.lisp ===================================== @@ -47,18 +47,29 @@ (assert-false fd) (assert-true (and (integerp errno) (plusp errno))))) -;; Darwin allows any number of X's (including 0!) in the template but -;; Linux requires exactly 6. Hence skip this test. -#-darwin (define-test mkstemp.bad-template (:tag :issues) (multiple-value-bind (fd errno) (unix::unix-mkstemp "test-") ;; The template doesn't have enough X's so the FD should be NIL, ;; and a positive Unix errno value should be returned. - ;; - ;; Note that Darwin allows any number of X's (including 0!) in the - ;; template but Linux requires exactly 6. + (assert-false fd) + (assert-true (and (integerp errno) (plusp errno))))) + +(define-test mkstemp.bad-template.2 + (:tag :issues) + (multiple-value-bind (fd errno) + (unix::unix-mkstemp "test-XXXXXXX") + ;; The template has too many X's so the FD should be NIL, and a + ;; positive Unix errno value should be returned. + (assert-false fd) + (assert-true (and (integerp errno) (plusp errno))))) + +(define-test mkstemp.bad-template.3 + (:tag :issues) + (multiple-value-bind (fd errno) + (unix::unix-mkstemp "test-XXXXXXa") + ;; The template doesn't end in X's (assert-false fd) (assert-true (and (integerp errno) (plusp errno))))) @@ -100,9 +111,6 @@ (assert-false result) (assert-true (and (integerp errno) (plusp errno))))) -;; Darwin allows any number of X's (including 0!) in the template but -;; Linux requires exactly 6. Hence skip this test. -#-darwin (define-test mkdtemp.bad-template (:tag :issues) (multiple-value-bind (result errno) @@ -111,3 +119,19 @@ (assert-false result) (assert-true (and (integerp errno) (plusp errno))))) +(define-test mkdtemp.bad-template.2 + (:tag :issues) + (multiple-value-bind (result errno) + (unix::unix-mkdtemp "dir-XXXXXXX") + ;; Too many X's in template. + (assert-false result) + (assert-true (and (integerp errno) (plusp errno))))) + +(define-test mkdtemp.bad-template.2 + (:tag :issues) + (multiple-value-bind (result errno) + (unix::unix-mkdtemp "dir-XXXXXXa") + ;; Template doesn't end in X's + (assert-false result) + (assert-true (and (integerp errno) (plusp errno))))) + View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/7275edf3ac0d0ab37d1dc79... -- View it on GitLab: https://gitlab.common-lisp.net/cmucl/cmucl/-/compare/7275edf3ac0d0ab37d1dc79... You're receiving this email because of your account on gitlab.common-lisp.net.
participants (1)
-
Raymond Toy (@rtoy)