Skip to content

duplication: Permission-check block copy-pasted three times across __open (×2) and __sysopen (×1) #330

@Koan-Bot

Description

@Koan-Bot

Problem

The pattern if (defined $_mock_uid) { if (defined contents) { check file perms } elsif (write mode) { check parent dir perms } } is duplicated verbatim in three places. There is also a subtle API inconsistency: the two __open blocks access $mock_file->contents() (the method, which handles dirs/symlinks specially) while __sysopen accesses $mock_file->{'contents'} directly (raw hash key). A future bug fix to one copy may not be applied to the others.

Why This Matters

Demonstrated already: the dead-branch bug above (#1) was introduced precisely because the __sysopen copy diverged from the __open copies. Three independent copies guarantee future drift.

Suggested Fix

Extract a private helper _check_open_perms($mock_file, $abs_path, $rw, $func_name) that encapsulates the three-way logic and calls _maybe_throw_autodie($func_name, @_) on failure. Return a boolean; callers return undef unless _check_open_perms(...). All three call sites reduce to one line.

Details

Severity 🟡 Medium
Category duplication
Location lib/Test/MockFile.pm:2815-2835, 3151-3170, 3355-3374
Effort 🛠️ Moderate effort

🤖 Created by Kōan from audit session

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