Skip to contentSkip to conversation

New: [truncate(2) man page] after extending file extra bytes are not always zero

Bug 220569 [email protected] 1 participant 1 message Sep 12, 2025 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

https://bugzilla.kernel.org/show_bug.cgi?id=220569

Bug ID: 220569
Summary: [truncate(2) man page] after extending file extra
bytes are not always zero
Product: Documentation
Version: unspecified
Hardware: AMD
OS: Linux
Status: NEW
Severity: normal
Priority: P3
Component: man-pages
Assignee: [email protected]
Reporter: [email protected]
Regression: No

truncate(2) man page has this sentence:

If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0').

but it's not always true

how to cause truncate to extend file with non-zero bytes (run all commands as root):

1. change working directory to ramfs mount (tmpfs same result):

mkdir r
mount -t ramfs asdf r
cd r

2. make create_weird_file.c file with content:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

int main(int argc,char**argv){
        if(argc!=2){
                puts("usage: create_weird_file /path/to/file");
                return 1;
        }
        int fd=open(argv[1],O_RDWR|O_CLOEXEC|O_CREAT|O_EXCL,0600);
        if(fd<0){
                printf("open error: %#m\n");
                return 1;
        }
        if(ftruncate(fd,1)){
                printf("ftruncate error: %#m\n");
                return 1;
        }
        void*mm=mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
        if(mm==MAP_FAILED){
                printf("mmap error: %#m\n");
                return 1;
        }
        *(int*)mm=1717859169;
}

3. make truncate_4.c file with content:

#include <stdio.h>
#include <unistd.h>

int main(int argc,char**argv){
        if(argc!=2){
                puts("usage: truncate_4 /path/to/file");
                return 1;
        }
        if(truncate(argv[1],4)){
                printf("truncate error: %#m\n");
                return 1;
        }
}

4. compile create_weird_file.c:

gcc -o create_weird_file create_weird_file.c

5. compile truncate_4.c

gcc -o truncate_4 truncate_4.c

6. create 1 byte file f:

./create_weird_file f

7. see file content and size:

xxd f

output for me is:

00000000: 61 a

8. truncate file f to 4 bytes:

./truncate_4 f

9. see file content and size:

xxd f

output for me is:

00000000: 6173 6466 asdf

extra 3 bytes of file are "sdf" instead of "\0\0\0"

tested on 2 operating systems:
kde neon unstable, uname -r -v -m -p -i -o: 6.14.0-29-generic #29~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Aug 14 16:52:50 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
opensuse tumbleweed, uname -r -v -m -p -i -o: 6.16.5-1-default #1 SMP PREEMPT_DYNAMIC Thu Sep 4 15:51:43 UTC 2025 (642f24d) x86_64 x86_64 x86_64 GNU/Linux

Signature · 4 lines
You may reply to this email to add a comment.

You are receiving this mail because:
You are watching the assignee of the bug.