rzetterberg
rzetterberg

Reputation: 10268

"Syscall param open(mode) contains uninitialised byte(s)" in open sys call

fd = open(pathname, O_WRONLY | O_LARGEFILE | O_APPEND | O_CREAT);

When running my tests I encounter a valgrind error:

==14280== Syscall param open(mode) contains uninitialised byte(s)
==14280==    at 0x4111084: open64 (open64.c:42)

And the return value of open is -2

Errno is set to 13 (Permission denied)

The pathname buffer contains:

0x68
0x72
0x32
0x2E
0x66
0x61
0x0 

Info about my system:

$ ls -la
drwxr-xr-x 7 rzetterberg zed 4096 Jul  4 13:56 .
$ id
uid=1000(rzetterberg) gid=1000(rzetterberg) groups=1000(rzetterberg), ... etc
$ sudo file -Ls /dev/sda1
/dev/sda1: sticky Linux rev 1.0 ext3 filesystem data, UUID=XXXX (needs journal recovery) (large files)
$ uname -a
Linux xxxx 3.2.0-4-686-pae #1 SMP Debian 3.2.46-1 i686 GNU/Linux

Headers included:

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

I don't understand how mode can have unitialised bytes and why I'm getting a Permission denied. It seems very strange to me!

Upvotes: 4

Views: 1984

Answers (2)

Art
Art

Reputation: 20392

When you call open with O_CREAT you need a third argument to the system call. That parameter is called mode. Valgrind is reporting uninitialized bytes because it doesn't know that you didn't specify the argument, but it sees that whatever happened to be on the stack isn't what it expects.

Upvotes: 2

caf
caf

Reputation: 239041

If you supply the O_CREAT flag to open(), you must supply a third argument. This is an integer which represents the permissions to apply to the created file (modified by the current umask).

Upvotes: 6

Related Questions