Skip to content

Commit

Permalink
prevent panic when registering barcode fails (jung-kurt#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmeyers authored and jung-kurt committed Jun 10, 2017
1 parent 1479a09 commit ed00acd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions contrib/barcode/barcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions contrib/barcode/barcode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package barcode_test

import (
"testing"

"github.com/boombuler/barcode/code128"
"github.com/boombuler/barcode/qr"
"github.com/jung-kurt/gofpdf"
Expand Down Expand Up @@ -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: é")
}

0 comments on commit ed00acd

Please sign in to comment.