From ed00acdb054d3e26b9a2ffdb09fdd94585bab65d Mon Sep 17 00:00:00 2001 From: Dan Meyers Date: Sat, 10 Jun 2017 13:54:41 -0400 Subject: prevent panic when registering barcode fails (#118) --- contrib/barcode/barcode.go | 10 ++++++---- contrib/barcode/barcode_test.go | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'contrib') diff --git a/contrib/barcode/barcode.go b/contrib/barcode/barcode.go index 63a4908..9f8d3ff 100644 --- a/contrib/barcode/barcode.go +++ b/contrib/barcode/barcode.go @@ -20,6 +20,11 @@ package barcode import ( "bytes" "errors" + "image/jpeg" + "io" + "strconv" + "sync" + "github.com/boombuler/barcode" "github.com/boombuler/barcode/aztec" "github.com/boombuler/barcode/codabar" @@ -31,10 +36,6 @@ import ( "github.com/boombuler/barcode/twooffive" "github.com/jung-kurt/gofpdf" "github.com/ruudk/golang-pdf417" - "image/jpeg" - "io" - "strconv" - "sync" ) // barcodes represents the barcodes that have been registered through this @@ -197,6 +198,7 @@ func RegisterTwoOfFive(pdf barcodePdf, code string, interleaved bool) string { func registerBarcode(pdf barcodePdf, bcode barcode.Barcode, err error) string { if err != nil { pdf.SetError(err) + return "" } return Register(bcode) diff --git a/contrib/barcode/barcode_test.go b/contrib/barcode/barcode_test.go index c3c648c..5b6c8cf 100644 --- a/contrib/barcode/barcode_test.go +++ b/contrib/barcode/barcode_test.go @@ -1,6 +1,8 @@ package barcode_test import ( + "testing" + "github.com/boombuler/barcode/code128" "github.com/boombuler/barcode/qr" "github.com/jung-kurt/gofpdf" @@ -150,3 +152,9 @@ func ExampleRegisterPdf417() { // Output: // Successfully generated ../../pdf/contrib_barcode_RegisterPdf417.pdf } + +// This test ensures that no panic arises when an invalid barcode is registered. +func TestRegisterCode128(t *testing.T) { + pdf := createPdf() + barcode.RegisterCode128(pdf, "Invalid character: é") +} -- cgit v1.2.1-24-ge1ad