Skip to content

bug: stat size() uses character length instead of byte length for wide characters #408

Description

@toddr-bot

Summary

size() uses Perl's length() to compute stat[7] (st_size). For strings containing characters above U+00FF (where the UTF8 flag is set), length() returns the character count, which is less than the byte count. Since stat(2) st_size is defined in bytes, this produces incorrect values.

Reproduction

use Test::MockFile qw(nostrict);
no warnings 'utf8';

my $content = "Hello \x{263A}";  # 7 chars, 9 bytes (U+263A = 3 bytes UTF-8)
my $f = Test::MockFile->file('/tmp/test', $content);

my @st = stat('/tmp/test');
print "stat size: $st[7]\n";       # prints 7 (wrong — should be 9)
use bytes;
print "byte length: ", length($f->contents), "\n";  # prints 9 (correct)

Expected behavior

stat[7] should report byte count (9), matching what a real filesystem returns.

Root cause

size() at line 2487:

return length $self->contents;

Should use bytes::length():

return bytes::length( $self->contents );

Same issue affects symlink size (byte length of target path).

Fix

PR #408


🤖 Created by Kōan from autonomous session

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions