A continuation of HomeBox the inventory and organization system built for the Home User
fix: clamp processedChars before slicing in wrapText (#1658)
Sai Asish Y2daf3009e90f
diff --git a/backend/pkgs/labelmaker/labelmaker.go b/backend/pkgs/labelmaker/labelmaker.goindex 23c4f2b4..9a83fc4a 100644--- a/backend/pkgs/labelmaker/labelmaker.go+++ b/backend/pkgs/labelmaker/labelmaker.go@@ -109,7 +109,7 @@ func wrapText(text string, face font.Face, maxWidth int, maxHeight int, lineHeigif !unlimitedHeight {currentHeight += lineHeightif currentHeight > maxHeight {- return wrappedLines[:len(wrappedLines)-1], text[processedChars:]+ return wrappedLines[:len(wrappedLines)-1], text[min(processedChars, len(text)):]}}continuediff --git a/backend/pkgs/labelmaker/labelmaker_test.go b/backend/pkgs/labelmaker/labelmaker_test.goindex 894cd8a7..5b2f7329 100644--- a/backend/pkgs/labelmaker/labelmaker_test.go+++ b/backend/pkgs/labelmaker/labelmaker_test.go@@ -1,12 +1,14 @@package labelmakerimport (+ "image""os""path/filepath""runtime""strings""testing"+ "github.com/golang/freetype/truetype""github.com/stretchr/testify/assert""github.com/stretchr/testify/require""github.com/sysadminsmedia/homebox/backend/internal/sys/config"@@ -256,3 +258,23 @@ func TestPrintLabel_ArgumentInjection(t *testing.T) {assert.Equal(t, maliciousTitle, got[0])assert.True(t, strings.HasSuffix(got[1], ".png"), "second arg should be the generated label file, got %q", got[1])}++func TestWrapText_TrailingEmptyLineAtHeightLimit(t *testing.T) {+ regularFont, err := loadFont(nil, FontTypeRegular)+ require.NoError(t, err)++ face := truetype.NewFace(regularFont, &truetype.Options{+ Size: 12,+ DPI: 72,+ })+ tmpImg := image.NewRGBA(image.Rect(0, 0, 1, 1))+ ctx := createContext(regularFont, 12, tmpImg, 72)++ // The height limit is crossed on the trailing empty line, where+ // processedChars has already advanced past len(text).+ text := "Widget A\n"+ lines, remaining := wrapText(text, face, 200, 12, 12, ctx)++ assert.Equal(t, []string{"Widget A"}, lines)+ assert.Empty(t, remaining)+}