Raymond Toy pushed to branch issue-375-mkstemp-return-filename at cmucl / cmucl

Commits:

2 changed files:

Changes:

  • src/code/unix.lisp
    ... ... @@ -2904,6 +2904,15 @@
    2904 2904
     			  (function (* char))))
    
    2905 2905
     	c-string))
    
    2906 2906
     
    
    2907
    +(defun check-template (template)
    
    2908
    +  ;; Make sure the template ends with exactly 6 X's and no more.
    
    2909
    +  (let ((last-non-x (position-if-not #'(lambda (c)
    
    2910
    +					 (char= c #\X))
    
    2911
    +				     template
    
    2912
    +				     :from-end t)))
    
    2913
    +    (and last-non-x
    
    2914
    +	 (= last-non-x (- (length template) 7)))))
    
    2915
    +
    
    2907 2916
     (defun unix-mkstemp (template)
    
    2908 2917
       _N"Generates a unique temporary file name from TEMPLATE, and creates
    
    2909 2918
       and opens the file.  On success, the corresponding file descriptor
    
    ... ... @@ -2911,6 +2920,10 @@
    2911 2920
       code is returned.
    
    2912 2921
     
    
    2913 2922
       The last six characters of the template must be \"XXXXXX\"."
    
    2923
    +  ;; Make sure the template is valid.
    
    2924
    +  (unless (check-template template)
    
    2925
    +    (return-from unix-mkstemp
    
    2926
    +      (values nil einval)))
    
    2914 2927
       (let* ((format (if (eql *filename-encoding* :null)
    
    2915 2928
     		     :iso8859-1
    
    2916 2929
     		     *filename-encoding*))
    
    ... ... @@ -2942,6 +2955,10 @@
    2942 2955
     
    
    2943 2956
       If the directory cannot be created NIL and the UNIX error code is
    
    2944 2957
       returned."
    
    2958
    +  ;; Make sure the template is valid.
    
    2959
    +  (unless (check-template template)
    
    2960
    +    (return-from unix-mkstemp
    
    2961
    +      (values nil einval)))
    
    2945 2962
       (let* ((format (if (eql *filename-encoding* :null)
    
    2946 2963
     		     :iso8859-1
    
    2947 2964
     		     *filename-encoding*))
    

  • tests/unix.lisp
    ... ... @@ -47,18 +47,29 @@
    47 47
         (assert-false fd)
    
    48 48
         (assert-true (and (integerp errno) (plusp errno)))))
    
    49 49
     
    
    50
    -;; Darwin allows any number of X's (including 0!) in the template but
    
    51
    -;; Linux requires exactly 6.  Hence skip this test.
    
    52
    -#-darwin
    
    53 50
     (define-test mkstemp.bad-template
    
    54 51
       (:tag :issues)
    
    55 52
       (multiple-value-bind (fd errno)
    
    56 53
           (unix::unix-mkstemp "test-")
    
    57 54
         ;; The template doesn't have enough X's so the FD should be NIL,
    
    58 55
         ;; and a positive Unix errno value should be returned.
    
    59
    -    ;;
    
    60
    -    ;; Note that Darwin allows any number of X's (including 0!) in the
    
    61
    -    ;; template but Linux requires exactly 6.
    
    56
    +    (assert-false fd)
    
    57
    +    (assert-true (and (integerp errno) (plusp errno)))))
    
    58
    +
    
    59
    +(define-test mkstemp.bad-template.2
    
    60
    +  (:tag :issues)
    
    61
    +  (multiple-value-bind (fd errno)
    
    62
    +      (unix::unix-mkstemp "test-XXXXXXX")
    
    63
    +    ;; The template has too many X's so the FD should be NIL, and a
    
    64
    +    ;; positive Unix errno value should be returned.
    
    65
    +    (assert-false fd)
    
    66
    +    (assert-true (and (integerp errno) (plusp errno)))))
    
    67
    +
    
    68
    +(define-test mkstemp.bad-template.3
    
    69
    +  (:tag :issues)
    
    70
    +  (multiple-value-bind (fd errno)
    
    71
    +      (unix::unix-mkstemp "test-XXXXXXa")
    
    72
    +    ;; The template doesn't end in X's
    
    62 73
         (assert-false fd)
    
    63 74
         (assert-true (and (integerp errno) (plusp errno)))))
    
    64 75
     
    
    ... ... @@ -100,9 +111,6 @@
    100 111
         (assert-false result)
    
    101 112
         (assert-true (and (integerp errno) (plusp errno)))))
    
    102 113
     
    
    103
    -;; Darwin allows any number of X's (including 0!) in the template but
    
    104
    -;; Linux requires exactly 6.  Hence skip this test.
    
    105
    -#-darwin
    
    106 114
     (define-test mkdtemp.bad-template
    
    107 115
       (:tag :issues)
    
    108 116
       (multiple-value-bind (result errno)
    
    ... ... @@ -111,3 +119,19 @@
    111 119
         (assert-false result)
    
    112 120
         (assert-true (and (integerp errno) (plusp errno)))))
    
    113 121
     
    
    122
    +(define-test mkdtemp.bad-template.2
    
    123
    +  (:tag :issues)
    
    124
    +  (multiple-value-bind (result errno)
    
    125
    +      (unix::unix-mkdtemp "dir-XXXXXXX")
    
    126
    +    ;; Too many X's in template.
    
    127
    +    (assert-false result)
    
    128
    +    (assert-true (and (integerp errno) (plusp errno)))))
    
    129
    +
    
    130
    +(define-test mkdtemp.bad-template.2
    
    131
    +  (:tag :issues)
    
    132
    +  (multiple-value-bind (result errno)
    
    133
    +      (unix::unix-mkdtemp "dir-XXXXXXa")
    
    134
    +    ;; Template doesn't end in X's
    
    135
    +    (assert-false result)
    
    136
    +    (assert-true (and (integerp errno) (plusp errno)))))
    
    137
    +