Linux Manual Pages development
 help / color / mirror / Atom feed
From: bugzilla-daemon@kernel.org
To: linux-man@vger.kernel.org
Subject: [Bug 220569] New: [truncate(2) man page] after extending file extra bytes are not always zero
Date: Fri, 12 Sep 2025 13:50:00 +0000	[thread overview]
Message-ID: <bug-220569-11311@https.bugzilla.kernel.org/> (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: 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.

                 reply	other threads:[~2025-09-12 13:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-220569-11311@https.bugzilla.kernel.org/ \
    --to=bugzilla-daemon@kernel.org \
    --cc=linux-man@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox