fastfetch-cli / fastfetch

An actively maintained, feature-rich and performance oriented, neofetch like system information tool.
MIT License
9.74k stars 390 forks source link

[BUG] Invalid named colors get prepended to affected text #1138

Closed loneways closed 1 month ago

loneways commented 1 month ago

General description:

When selecting an invalid named color for the text (either with --color or in the config file), it gets added to the affected text minus its first letter plus a 'm' (i.e., "fastfetch --color yello" would prepend "ello"). Sometimes, even numbers and other symbols are added This is a rather trivial bug (only extra characters being added)

What happened:

(Screenshots below) I first tried to change the color of both the title and keys with

fastfetch --color purple

which, obviously, does not work, since ANSI does not have a "purple" color, but rather "magenta". The thing is, "urplem" was prepended to both the title and the keys, which should not have happened. The output remained correct, though.

Testing with different named colors (with or without prefix) gave the same result: valid colors worked, invalid named colors had their name prepended to the text minus their first letter plus a 'm', which was curious.

What should happen:

No characters should be prepended. The color of the affected text should (probably) be set instead to the one in the configuration file, or the default one

Fastfetch version used:

2.20.0, which is the latest version

Did it work in an older version:

I can't tell for certain, since I've only started using fastfetch very recently.

I did pick an old version from the releases page (more specifically, 2.8.3), and the bug still remained

Where did you get the binary:

Compiled both from the master and dev branch, with both having this bug

Does this issue still occur in the latest dev build:

Yes

Screenshots:

Valid named color (magenta): image

Invalid named color (magent and purple): image image

The content of the configuration file you use (if any):

{
  "$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
  "modules": [
    "title",
    "separator",
    "os",
    "host",
    "kernel",
    "uptime",
    "packages",
    "shell",
    "display",
    "de",
    "wm",
    "wmtheme",
    "theme",
    "icons",
    "font",
    "cursor",
    "terminal",
    "terminalfont",
    "cpu",
    "gpu",
    "memory",
    "swap",
    "disk",
    "battery",
    "poweradapter",
    "locale",
    "break",
    "break"
  ]
}

Output of `fastfetch -c ci.jsonc --format json (with unnecessary or sensitive information removed):

[
  {
    "type": "Title",
    "result": {
      "userName": "loneways",
      "hostName": "Ravenoth",
      "homeDir": "/home/loneways/",
      "exePath": "/home/loneways/Público/fastfetch/build/fastfetch",
      "userShell": "/bin/bash"
    },
    "stat": 0
  },
  {
    "type": "Separator",
    "error": "Unsupported for JSON format",
    "stat": 0
  },
  {
    "type": "OS",
    "result": {
      "buildID": "",
      "codename": "bookworm",
      "id": "debian",
      "idLike": "",
      "name": "Debian GNU/Linux",
      "prettyName": "Debian GNU/Linux 12 (bookworm)",
      "variant": "",
      "variantID": "",
      "version": "12.6",
      "versionID": "12.6"
    },
    "stat": 0
  },
  {
    "type": "Host",
    "result": {
      "family": "103C_5336AN HP EliteBook",
      "name": "HP EliteBook 840 G4",
      "version": "",
      "sku": "X3V02AV",
      "vendor": "HP",
      "serial": "",
      "uuid": ""
    },
    "stat": 1
  },
  {
    "type": "Bios",
    "result": {
      "date": "01/25/2018",
      "release": "1.15",
      "vendor": "HP",
      "version": "P78 Ver. 01.15",
      "type": "UEFI"
    },
    "stat": 0
  },
  {
    "type": "Bootmgr",
    "result": {
      "name": "debian",
      "firmware": "\\EFI\\debian\\shimx64.efi",
      "secureBoot": false
    },
    "stat": 1
  },
  {
    "type": "Board",
    "result": {
      "name": "828C",
      "vendor": "HP",
      "version": "KBC Version 45.3F",
      "serial": ""
    },
    "stat": 0
  },
  {
    "type": "Chassis",
    "result": {
      "type": "Notebook",
      "vendor": "HP",
      "version": "",
      "serial": ""
    },
    "stat": 0
  },
  {
    "type": "Kernel",
    "result": {
      "architecture": "x86_64",
      "name": "Linux",
      "release": "6.1.0-23-amd64",
      "version": "#1 SMP PREEMPT_DYNAMIC Debian 6.1.99-1 (2024-07-15)",
      "displayVersion": "",
      "pageSize": 4096
    },
    "stat": 0
  },
  {
    "type": "InitSystem",
    "result": {
      "name": "systemd",
      "exe": "/usr/lib/systemd/systemd",
      "version": "252.26-1~deb12u2",
      "pid": 1
    },
    "stat": 11
  },
  {
    "type": "Loadavg",
    "result": [
      0.48095703125,
      0.68701171875,
      0.87060546875
    ],
    "stat": 0
  },
  {
    "type": "Processes",
    "result": 172,
    "stat": 0
  },
  {
    "type": "Packages",
    "result": {
      "all": 1700,
      "apk": 0,
      "brew": 0,
      "brewCask": 0,
      "choco": 0,
      "dpkg": 1700,
      "emerge": 0,
      "eopkg": 0,
      "flatpakSystem": 0,
      "flatpakUser": 0,
      "nixDefault": 0,
      "nixSystem": 0,
      "nixUser": 0,
      "pacman": 0,
      "paludis": 0,
      "pkg": 0,
      "pkgtool": 0,
      "macports": 0,
      "rpm": 0,
      "scoop": 0,
      "snap": 0,
      "winget": 0,
      "xbps": 0,
      "opkg": 0,
      "am": 0,
      "sorcery": 0,
      "guixSystem": 0,
      "guixUser": 0,
      "guixHome": 0,
      "pacmanBranch": ""
    },
    "stat": 2
  },
  {
    "type": "Shell",
    "result": {
      "exe": "/bin/bash",
      "exeName": "bash",
      "exePath": "/usr/bin/bash",
      "pid": 21957,
      "ppid": 3824,
      "processName": "bash",
      "prettyName": "bash",
      "version": "5.2.15",
      "tty": 1
    },
    "stat": 2
  },
  {
    "type": "Editor",
    "error": "$VISUAL or $EDITOR not set",
    "stat": 0
  },
  {
    "type": "Display",
    "result": [
      {
        "id": 0,
        "name": "CMN14C0",
        "primary": false,
        "output": {
          "width": 1920,
          "height": 1080
        },
        "scaled": {
          "width": 0,
          "height": 0
        },
        "physical": {
          "width": 308,
          "height": 173
        },
        "refreshRate": 60.007852375343546,
        "rotation": 0,
        "bitDepth": 0,
        "hdrEnabled": false,
        "wcgEnabled": false,
        "type": "Unknown"
      }
    ],
    "stat": 2
  },
  {
    "type": "Brightness",
    "result": [
      {
        "name": "CMN14C0",
        "max": 7500.0,
        "min": 0.0,
        "current": 3376.0
      }
    ],
    "stat": 0
  },
  {
    "type": "Monitor",
    "result": [
      {
        "hdrCompatible": false,
        "name": "CMN14C0",
        "resolution": {
          "width": 1920,
          "height": 1080
        },
        "physical": {
          "height": 173,
          "width": 308
        },
        "refreshRate": 0.0,
        "manufactureDate": {
          "year": 2015,
          "week": 20
        },
        "serial": null
      }
    ],
    "stat": 0
  },
  {
    "type": "LM",
    "result": {
      "service": "sddm",
      "type": "Wayland",
      "version": ""
    },
    "stat": 0
  },
  {
    "type": "DE",
    "result": {
      "processName": "plasmashell",
      "prettyName": "KDE Plasma",
      "version": "5.27.5"
    },
    "stat": 0
  },
  {
    "type": "WM",
    "result": {
      "processName": "kwin_wayland_wrapper",
      "prettyName": "KWin",
      "protocolName": "Wayland",
      "pluginName": ""
    },
    "stat": 0
  },
  {
    "type": "WMTheme",
    "result": "Brisa",
    "stat": 0
  },
  {
    "type": "Theme",
    "result": {
      "theme1": "Breeze (Dark) [Qt]",
      "theme2": "Breeze [GTK2/3]"
    },
    "stat": 1
  },
  {
    "type": "Icons",
    "result": {
      "icons1": "breeze-dark [Qt]",
      "icons2": "breeze-dark [GTK2/3/4]"
    },
    "stat": 0
  },
  {
    "type": "Font",
    "result": {
      "display": "Noto Sans (14pt) [Qt], Noto Sans (10pt) [GTK2/3/4]",
      "fonts": [
        "Noto Sans (14pt)",
        "Noto Sans (10pt)",
        "Noto Sans (10pt)",
        "Noto Sans (10pt)"
      ]
    },
    "stat": 0
  },
  {
    "type": "Cursor",
    "result": {
      "theme": "breeze_cursors",
      "size": "24"
    },
    "stat": 0
  },
  {
    "type": "Terminal",
    "result": {
      "processName": "konsole",
      "exe": "/usr/bin/konsole",
      "exeName": "konsole",
      "exePath": "/usr/bin/konsole",
      "pid": 3824,
      "ppid": 851,
      "prettyName": "konsole",
      "version": "22.12.3",
      "tty": "/dev/pts/1"
    },
    "stat": 0
  },
  {
    "type": "TerminalFont",
    "result": {
      "font": {
        "name": "Mononoki Nerd Font",
        "size": "18",
        "styles": [],
        "pretty": "Mononoki Nerd Font (18pt)"
      },
      "fallback": {
        "name": "",
        "size": "",
        "styles": [],
        "pretty": ""
      }
    },
    "stat": 0
  },
  {
    "type": "TerminalSize",
    "result": {
      "columns": 147,
      "rows": 37,
      "width": 1918,
      "height": 1040
    },
    "stat": 0
  },
  {
    "type": "TerminalTheme",
    "result": {
      "fg": {
        "r": 252,
        "g": 252,
        "b": 252,
        "dark": false
      },
      "bg": {
        "r": 35,
        "g": 38,
        "b": 39,
        "dark": true
      }
    },
    "stat": 0
  },
  {
    "type": "CPU",
    "result": {
      "cpu": "Intel(R) Core(TM) i5-7200U",
      "vendor": "GenuineIntel",
      "cores": {
        "physical": 2,
        "logical": 4,
        "online": 4
      },
      "frequency": {
        "base": 2500,
        "max": 2500,
        "biosLimit": 0
      },
      "coreTypes": [
        {
          "count": 4,
          "freq": 2500
        }
      ],
      "temperature": 35.0
    },
    "stat": 10
  },
  {
    "type": "CPUCache",
    "result": {
      "l1": [
        {
          "size": 32768,
          "num": 2,
          "lineSize": 64,
          "type": "data"
        },
        {
          "size": 32768,
          "num": 2,
          "lineSize": 64,
          "type": "instruction"
        }
      ],
      "l2": [
        {
          "size": 262144,
          "num": 2,
          "lineSize": 64,
          "type": "unified"
        }
      ],
      "l3": [
        {
          "size": 3145728,
          "num": 1,
          "lineSize": 64,
          "type": "unified"
        }
      ]
    },
    "stat": 2
  },
  {
    "type": "CPUUsage",
    "result": [
      0.0,
      0.0,
      0.0,
      0.0
    ],
    "stat": 200
  },
  {
    "type": "GPU",
    "result": [
      {
        "coreCount": null,
        "memory": {
          "dedicated": {
            "total": null,
            "used": null
          },
          "shared": {
            "total": null,
            "used": null
          }
        },
        "driver": "i915",
        "name": "HD Graphics 620",
        "temperature": null,
        "type": "Integrated",
        "vendor": "Intel",
        "platformApi": "DRM (card0)",
        "frequency": 1000.0,
        "deviceId": 91224
      }
    ],
    "stat": 4
  },
  {
    "type": "Memory",
    "result": {
      "total": 8200187904,
      "used": 3373535232
    },
    "stat": 0
  },
  {
    "type": "PhysicalMemory",
    "error": "Failed to get SMBIOS data",
    "stat": 0
  },
  {
    "type": "Swap",
    "result": {
      "total": 1024454656,
      "used": 0
    },
    "stat": 0
  },
  {
    "type": "Disk",
    "result": [
      {
        "bytes": {
          "available": 206677348352,
          "free": 219419013120,
          "total": 249365385216,
          "used": 29946372096
        },
        "files": {
          "total": 15540224,
          "used": 219769
        },
        "filesystem": "ext4",
        "mountpoint": "/",
        "mountFrom": "/dev/nvme0n1p2",
        "name": "",
        "volumeType": [
          "Regular"
        ],
        "createTime": "2024-07-30T20:13:53.000+0100"
      },
      {
        "bytes": {
          "available": 529690624,
          "free": 529690624,
          "total": 535805952,
          "used": 6115328
        },
        "files": {
          "total": null,
          "used": null
        },
        "filesystem": "vfat",
        "mountpoint": "/boot/efi",
        "mountFrom": "/dev/nvme0n1p1",
        "name": "",
        "volumeType": [
          "Hidden"
        ],
        "createTime": null
      }
    ],
    "stat": 1
  },
  {
    "type": "Battery",
    "result": [
      {
        "capacity": 58.0,
        "manufacturer": "Hewlett-Packard",
        "manufactureDate": "",
        "modelName": "Primary",
        "status": "Discharging",
        "technology": "Li-ion",
        "serial": "08482 2017/08/10",
        "temperature": null,
        "cycleCount": 0
      }
    ],
    "stat": 9
  },
  {
    "type": "PowerAdapter",
    "result": [],
    "stat": 0
  },
  {
    "type": "Player",
    "error": "Fastfetch was compiled without DBus support",
    "stat": 0
  },
  {
    "type": "Media",
    "error": "Fastfetch was compiled without DBus support",
    "stat": 0
  },
  {
    "type": "Locale",
    "result": "pt_PT.UTF-8",
    "stat": 0
  },
  {
    "type": "Vulkan",
    "error": "fastfetch was compiled without vulkan support",
    "stat": 0
  },
  {
    "type": "OpenGL",
    "error": "Fastfetch was built without gl support.",
    "stat": 0
  },
  {
    "type": "OpenCL",
    "error": "fastfetch was compiled without OpenCL support",
    "stat": 0
  },
  {
    "type": "Bluetooth",
    "error": "Fastfetch was compiled without DBus support",
    "stat": 0
  },
  {
    "type": "Sound",
    "error": "Fastfetch was built without libpulse support",
    "stat": 0
  },
  {
    "type": "NetIO",
    "result": [
      {
        "name": "wlp2s0",
        "defaultRoute": true,
        "txBytes": 1450,
        "rxBytes": 1558,
        "txPackets": 14,
        "rxPackets": 11,
        "rxErrors": 0,
        "txErrors": 0,
        "rxDrops": 0,
        "txDrops": 0
      }
    ],
    "stat": 488
  },
  {
    "type": "DiskIO",
    "result": [
      {
        "name": "SAMSUNG MZVLW256HEHP-000H1",
        "devPath": "/dev/nvme0n1",
        "bytesRead": 0,
        "bytesWritten": 1789952,
        "readCount": 0,
        "writeCount": 338
      }
    ],
    "stat": 1
  },
  {
    "type": "PhysicalDisk",
    "result": [
      {
        "name": "SAMSUNG MZVLW256HEHP-000H1",
        "devPath": "/dev/nvme0n1",
        "interconnect": "NVMe",
        "kind": "SSD",
        "size": 256060514304,
        "serial": "S340NA0JA18112",
        "removable": false,
        "readOnly": false,
        "revision": "CXB73H1Q",
        "temperature": 31.85
      }
    ],
    "stat": 0
  },
  {
    "type": "Version",
    "result": {
      "projectName": "fastfetch",
      "sysName": "Linux",
      "architecture": "x86_64",
      "version": "2.20.0",
      "versionTweak": "",
      "cmakeBuiltType": "RelWithDebInfo",
      "compileTime": "Jul 31 2024, 16:23:16",
      "compiler": "gcc 12.2.0",
      "debugMode": false,
      "libc": "glibc 2.36"
    },
    "stat": 0
  },
  {
    "type": "Break",
    "error": "Unsupported for JSON format",
    "stat": 0
  },
  {
    "type": "Colors",
    "error": "Unsupported for JSON format",
    "stat": 0
  }
]

Output of fastfetch --list-features:

threads
linux/videodev2
linux/wireless

Possible solution

After searching around in the code, I found what I believe is the problem

Line 210 in the file src/common/option.c

ffStrbufAppendC(buffer, *value);

After commenting out this line and recompiling, using commands like:

fastfetch --color purple
fastfetch --color blu
fastfetch --color mgent

doesn't prepend extra characters. However,

fastfetch --color '12;34'

and similar commands don't work anymore, so that line seems essential. After some experiments, I arrived to another solution: check if value has a number and, if so, execute the line mentioned above. This seemed to work - applying invalid name colors no longer adds extra characters and ANSI escape sequences (like 35; 5; 38:) work again. The same goes for the values in the configuration file

In order to accomplish this, I used a regex ("[0-9]"), which would require importing another library which may not be great. A for loop could be used instead. Of course, this would mean the value can be iterated several times, though I haven't noticed any difference in the performance

Sorry for writing a whole dissertation just for a trivial bug. Since I came up with a potential solution, should I create a pull request with it? I'm not used to contributing to open source

CarterLi commented 1 month ago

It will now report an error