Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure nil is accepted as FS config #1182

Merged
merged 1 commit into from
Mar 3, 2023

Conversation

evacchi
Copy link
Contributor

@evacchi evacchi commented Mar 1, 2023

In previous releases, passing a nil value as an FS config did not cause any error. However, in 1.0.0-rc.1 this leads
to the creation of an invalid adapter{fs: nil}, which eventually leads to a panic (nil):

(f *FileEntry) Stat(st *platform.Stat_t) =>
    (r *lazyDir) file() =>
        r.fs.OpenFile(".", os.O_RDONLY, 0)

with fs == nil

The backwards-compatible fix is to make Adapt() return UnimplementedFS.

However, we may want to consider returning an error somewhere, because configuring a nil FS may be unintended.

Signed-off-by: Edoardo Vacchi evacchi@users.noreply.github.com

Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is cleaner to handle this at the call site. The adapter is too late imho, and nil there a bug not an intended behavior. otoh keeping the nil guard in adapt is also fine!

so main change I prefer is to check nil inside here:

// WithFS implements ModuleConfig.WithFS
func (c *moduleConfig) WithFS(fs fs.FS) ModuleConfig {
	return c.WithFSConfig(NewFSConfig().WithFSMount(fs, ""))
}

to

// WithFS implements ModuleConfig.WithFS
func (c *moduleConfig) WithFS(fs fs.FS) ModuleConfig {
	var config FSConfig
	if fs != nil {
		config = NewFSConfig().WithFSMount(fs, "")
	}
	return c.WithFSConfig(config)
}

@evacchi
Copy link
Contributor Author

evacchi commented Mar 2, 2023

I originally did it that way and figured that it would do the same if I handled at Adapt; but it's ok to be a tad more defensive. I pushed the following, which avoids invoking withFSConfig() when fs is nil

// WithFS implements ModuleConfig.WithFS
func (c *moduleConfig) WithFS(fs fs.FS) ModuleConfig {
	if fs == nil {
		return c
	}
	return c.WithFSConfig(NewFSConfig().WithFSMount(fs, ""))
}

@codefromthecrypt
Copy link
Contributor

sorry to be a nitpicker, but any WithXXX(nil) should clear previous don't you think?

@evacchi
Copy link
Contributor Author

evacchi commented Mar 2, 2023

oh right I didn't think about that case!

In previous releases, passing a `nil` value as an FS config
did not cause any error. However, in 1.0.0-rc.1 this leads
to the creation of an invalid `adapter{fs: nil}`, which
eventually leads to a panic (nil):

    (f *FileEntry) Stat(st *platform.Stat_t) =>
        (r *lazyDir) file() =>
            r.fs.OpenFile(".", os.O_RDONLY, 0)

with fs == nil

The backwards-compatible fix is to make Adapt()
return UnimplementedFS, and ensuring `nil` is a valid value
that config is able to handle.

However, we may want to consider returning an error somewhere,
because configuring a nil FS may be unintended.

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bearing with me!

@codefromthecrypt codefromthecrypt merged commit f36d30b into tetratelabs:main Mar 3, 2023
@evacchi evacchi deleted the nil-fs-config branch February 12, 2024 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants