TryBugHunt
goType errorHard

Colour picker rejects any colour brighter than 7f

BugFind the bug and fix the code

What it should do

hexToRGB(hex) turns a colour like "#ff8000" into its red, green and blue values from 0 to 255. hexToRGB("#ff8000") returns [255, 128, 0].

What happens instead

hexToRGB("#ff8000") returns the error strconv.ParseInt: parsing "ff": value out of range.

package main

import (
    "errors"
    "strconv"
)

func hexToRGB(hex string) ([]int, error) {
    if len(hex) != 7 || hex[0] != '#' {
        return nil, errors.New("color must look like #rrggbb")
    }
    rgb := []int{}
    for i := 1; i < 7; i += 2 {
        v, err := strconv.ParseInt(hex[i:i+2], 16, 8)
        if err != nil {
            return nil, err
        }
        rgb = append(rgb, int(v))
    }
    return rgb, nil
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the type errors pattern

Integer division that drops the fraction, overflow, and conversions that change the value.

What you'll practise

A hard Go debugging exercise about type errors: integer division that drops the fraction, overflow, and conversions that change the value.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

The third argument to ParseInt is the bit size of the result, and ParseInt is signed. A signed 8-bit integer only goes up to 127, so any channel from 80 to ff (128 to 255) is reported as out of range. Dark colours like #102030 pass, which makes the bug look like it depends on the colour. A byte from 0 to 255 is an unsigned 8-bit value, so ParseUint with bit size 8 is the right call. The bit size argument is a range check, so match it to the range of the data, not just its width.

The pattern behind it: type errors.

More type errors to practise

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/797c0e9e-2ba8-4fa0-8694-5ba523194a79" title="Debugging challenge: Colour picker rejects any colour brighter than 7f" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/797c0e9e-2ba8-4fa0-8694-5ba523194a79">TryBugHunt</a></p>