fix: prevent TypeAssertionError panic when filetype setting is non-string (issue #4042)#4043
Open
dreamcreated wants to merge 1 commit intomicro-editor:masterfrom
Conversation
…ring
When saving a file to a new path (newPath=true), ReloadSettings is called
with reloadFiletype=true. If the user config contains a glob or ft: block
that somehow places a non-string value under the 'filetype' key (e.g. a
map[string]interface{} from a nested config entry), the unchecked type
assertions b.Settings["filetype"].(string) panic at runtime.
This was reproducible with kitty+nushell (issue micro-editor#4042):
$ micro → CTRL-s → f → crash
Fixes:
- Add a settingAsString helper in settings.go that safely converts
a settings value to string, falling back to a default on type mismatch
- Replace all bare .(string) assertions on b.Settings["filetype"] in
buffer.go (UpdateRules, FileType, buffer init) and settings.go
(ReloadSettings) with settingAsString calls
- In ReloadSettings, only assign settings["filetype"] to b.Settings if
the value is actually a string, silently ignoring non-string values
Fixes micro-editor#4042
Contributor
Andriamanitra
left a comment
There was a problem hiding this comment.
I don't think this is fixing the root cause of the problem. Instead of allowing nonsensical values for settings we should reject them in validateParsedSettings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4042 —
Crash when savingwith kitty+nushell.Reproduction:
Root Cause
When saving a file to a new path (
newPath=true),buffer.ReloadSettings(true)is called. The function reads the parsed config and callsUpdatePathGlobLocals, which may populatesettings["filetype"]with amap[string]interface{}value (from a glob-based config block) instead of a plain string.The subsequent bare type assertion
b.Settings["filetype"].(string)then panics. The same issue exists inUpdateRules(),FileType(), and the buffer initialisation path.Fix
settingAsString(v any, defaultVal string) stringhelper insettings.gothat safely converts a settings value to string, falling back to a safe default on type mismatch..(string)assertions onb.Settings["filetype"]inbuffer.goandsettings.gowithsettingAsStringcalls.ReloadSettings, added an explicit type guard when assigning from parsed settings — non-stringfiletypevalues are silently ignored (the value stays"unknown", which letsUpdateRulespick the right filetype by extension).Testing
go build ./...passes ✅go test ./internal/buffer/...— all 14 tests pass ✅Files Changed
internal/buffer/settings.go— helper + safe assertions inReloadSettingsinternal/buffer/buffer.go— safe assertions inUpdateRules,FileType, buffer init