Repository: microsoft/PowerToys
File: src/modules/AdvancedPaste/AdvancedPaste/Helpers/JsonHelper.cs (C#)

Code before:
            {
                if (string.IsNullOrEmpty(jsonText))
                {
                    var ini = new Dictionary<string, Dictionary<string, string>>();
                    var lastSectionName = string.Empty;

                    string[] lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                    // Validate content as ini (First line is a section name and second line is a key-value-pair.)
                    if (lines.Length >= 2 && IniSectionNameRegex.IsMatch(lines[0]) && IniValueLineRegex.IsMatch(lines[1]))
                    {
                        // Parse and convert Ini
                        foreach (string line in lines)
                        {
                            Match lineSectionNameCheck = IniSectionNameRegex.Match(line);
                            Match lineKeyValuePairCheck = IniValueLineRegex.Match(line);

                            if (lineSectionNameCheck.Success)
                            {
                                // Section name (Group 1)
                                lastSectionName = lineSectionNameCheck.Groups[1].Value;
                                ini.Add(lastSectionName, new Dictionary<string, string>());
                            }
                            else if (string.IsNullOrEmpty(lastSectionName) || !lineKeyValuePairCheck.Success)
                            {
                                // Fail if lastSectionName is still empty and not key-value-pair.
                                // (With empty lastSectionName we can't parse key-value-pairs
                                //  and if it is not a key-value-pair then the line is invalid.)
                                throw new FormatException("Invalid ini file format.");
                            }
                            else
                            {
                                // Key-value-pair (Group 1=Key; Group 2=Value)
                                ini[lastSectionName].Add(lineKeyValuePairCheck.Groups[1].Value, lineKeyValuePairCheck.Groups[2].Value);
                            }
                        }

                        // Convert to JSON
                        Logger.LogDebug("Converted from Ini.");
                        jsonText = JsonConvert.SerializeObject(ini, Newtonsoft.Json.Formatting.Indented);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("Failed parsing input as ini", ex);
            }

            // Try convert CSV
            try
            {

Review comment (bug):
```suggestion
                                // Fail if lastSectionName is still empty or not key-value-pair.
```


Code after:
            {
                if (string.IsNullOrEmpty(jsonText))
                {
                    var ini = new Dictionary<string, Dictionary<string, string>>();
                    var lastSectionName = string.Empty;

                    string[] lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                    // Skipp comment lines.
                    // (Comments are lines that starts with a semicolon.
                    // This also skips commented key-value-pairs.)
                    lines = lines.Where(l => !l.StartsWith(';')).ToArray();

                    // Validate content as ini
                    // (First line is a section name and second line is a section name or a key-value-pair.
                    // For the second line we check both, in case the first ini section is empty.)
                    if (lines.Length >= 2 && IniSectionNameRegex.IsMatch(lines[0]) &&
                        (IniSectionNameRegex.IsMatch(lines[1]) || IniValueLineRegex.IsMatch(lines[1])))
                    {
                        // Parse and convert Ini
                        foreach (string line in lines)
                        {
                            Match lineSectionNameCheck = IniSectionNameRegex.Match(line);
                            Match lineKeyValuePairCheck = IniValueLineRegex.Match(line);

                            if (lineSectionNameCheck.Success)
                            {
                                // Section name (Group 1)
                                lastSectionName = lineSectionNameCheck.Groups[1].Value.Trim();
                                if (string.IsNullOrWhiteSpace(lastSectionName))
                                {
                                    throw new FormatException("Invalid ini file format: Empty section name.");
                                }

                                ini.Add(lastSectionName, new Dictionary<string, string>());
                            }
                            else if (!lineKeyValuePairCheck.Success)
                            {
                                // Fail if it is not a key-value-pair (and was not detected as section name before).
                                throw new FormatException("Invalid ini file format: Invalid line.");
                            }
                            else
                            {
                                // Key-value-pair (Group 1=Key; Group 2=Value)
                                string iniKeyName = lineKeyValuePairCheck.Groups[1].Value.Trim();
                                if (string.IsNullOrWhiteSpace(iniKeyName))
                                {
                                    throw new FormatException("Invalid ini file format: Empty value name (key).");
                                }

                                string iniValueData = lineKeyValuePairCheck.Groups[2].Value;