Skip to content

fix: prevent TypeAssertionError panic when filetype setting is non-string (issue #4042)#4043

Open
dreamcreated wants to merge 1 commit intomicro-editor:masterfrom
dreamcreated:fix/type-assertion-panic-filetype-settings
Open

fix: prevent TypeAssertionError panic when filetype setting is non-string (issue #4042)#4043
dreamcreated wants to merge 1 commit intomicro-editor:masterfrom
dreamcreated:fix/type-assertion-panic-filetype-settings

Conversation

@dreamcreated
Copy link

Summary

Fixes #4042Crash when saving with kitty+nushell.

Reproduction:

$ micro → CTRL-s → f
Micro encountered an error: *runtime.TypeAssertionError interface conversion: interface {} is map[string]interface {}, not string

Root Cause

When saving a file to a new path (newPath=true), buffer.ReloadSettings(true) is called. The function reads the parsed config and calls UpdatePathGlobLocals, which may populate settings["filetype"] with a map[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 in UpdateRules(), FileType(), and the buffer initialisation path.

Fix

  1. Added a settingAsString(v any, defaultVal string) string helper in settings.go that safely converts a settings value to string, falling back to a safe default on type mismatch.
  2. Replaced all 4 unchecked .(string) assertions on b.Settings["filetype"] in buffer.go and settings.go with settingAsString calls.
  3. In ReloadSettings, added an explicit type guard when assigning from parsed settings — non-string filetype values are silently ignored (the value stays "unknown", which lets UpdateRules pick the right filetype by extension).

Testing

  • go build ./... passes ✅
  • go test ./internal/buffer/... — all 14 tests pass ✅
  • Build produces a working binary (verified no compilation errors)

Files Changed

  • internal/buffer/settings.go — helper + safe assertions in ReloadSettings
  • internal/buffer/buffer.go — safe assertions in UpdateRules, FileType, buffer init

…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
Copy link
Contributor

@Andriamanitra Andriamanitra left a comment

Choose a reason for hiding this comment

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

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.

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.

Crash when saving

2 participants