diff mbox series

[uclibc-ng-devel] mkostemp64: clear flags, as mkostemp does

Message ID 20181029041738.31984-1-casantos@datacom.com.br
State Accepted
Headers show
Series [uclibc-ng-devel] mkostemp64: clear flags, as mkostemp does | expand

Commit Message

Carlos Santos Oct. 29, 2018, 4:17 a.m. UTC
This should have been made in commit 9649721950 but was forgotten.

Signed-off-by: Carlos Santos <casantos@datacom.com.br>
---
Test:

    $ cat test.c
    #include <errno.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>

    void fatal(int n)
    {
        fprintf(stderr, "fatal[%d]: %s\n", n, strerror(errno));
        exit(1);
    }

    int main(int argc, char *argv[])
    {
        FILE *f;
        int cur_mode;
        char template[64];
        strncpy(template, argc > 1 ? argv[1] : "testXXXXXX", 63);

        umask(077);
        printf("main[1]: %s, %07o\n", template, O_WRONLY|O_CLOEXEC);
        int fd = mkostemp(template, O_WRONLY|O_CLOEXEC);
        if (fd < 0) {
            fatal(1);
        }
        cur_mode = fcntl(fd, F_GETFL);
        printf("main[2]: %s, %07o\n", template, cur_mode);

        printf("main[3]: %d, %s\n", fd, "we");
        f = fdopen(fd, "we");
        if (!f) {
            fatal(2);
        }

        if (fclose(f) == EOF) {
            fatal(3);
        }

        return 0;
    }

$ x86_64-buildroot-linux-uclibc-cc -Wall -Werror -D_GNU_SOURCE \
    -o mkostemp-test test.c
$ x86_64-buildroot-linux-uclibc-cc -Wall -Werror -D_GNU_SOURCE \
    -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
    -o mkostemp-test-large test.c

[ copy executables to target device (qemu, in this case) ]

Without patch:
    # mkostemp-test
    main[1]: testXXXXXX, 2000001
    random: fast init done
    main[2]: testIPn9UR, 0100002
    main[3]: 3, we
    # mkostemp-test-large
    main[1]: testXXXXXX, 2000001
    main[2]: testiXRydb, 0100003
    main[3]: 3, we
    fatal[2]: Invalid argument

With patch:
    # mkostemp-test
    main[1]: testXXXXXX, 2000001
    main[2]: testcVhbXs, 0100002
    main[3]: 3, we
    # mkostemp-test-large
    main[1]: testXXXXXX, 2000001
    main[2]: testDAulBF, 0100002
    main[3]: 3, we

---
Change-Id: I07226e166de1d8d7b8398cd54939ad43768352cf
---
 libc/stdlib/mkostemp64.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libc/stdlib/mkostemp64.c b/libc/stdlib/mkostemp64.c
index aa9736cd6..f4674bb0c 100644
--- a/libc/stdlib/mkostemp64.c
+++ b/libc/stdlib/mkostemp64.c
@@ -15,9 +15,9 @@ 
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <fcntl.h>
 #include "../misc/internals/tempname.h"
 
 /* Generate a unique temporary file name from TEMPLATE.
@@ -27,6 +27,7 @@ 
 int
 mkostemp64 (char *template, int flags)
 {
+  flags -= flags & O_ACCMODE; /* Remove O_RDONLY, O_WRONLY, and O_RDWR. */
   return __gen_tempname (template, __GT_BIGFILE, flags | O_LARGEFILE, 0,
                          S_IRUSR | S_IWUSR);
 }