* [Bug 220569] New: [truncate(2) man page] after extending file extra bytes are not always zero
@ 2025-09-12 13:50 bugzilla-daemon
0 siblings, 0 replies; only message in thread
From: bugzilla-daemon @ 2025-09-12 13:50 UTC (permalink / raw)
To: linux-man
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: documentation_man-pages@kernel-bugs.osdl.org
Reporter: alanas.00+k@mail.ru
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
--
You may reply to this email to add a comment.
You are receiving this mail because:
You are watching the assignee of the bug.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-09-12 13:50 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-12 13:50 [Bug 220569] New: [truncate(2) man page] after extending file extra bytes are not always zero bugzilla-daemon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox