From 52d82c282a974a0f057567ce5bb27e3cf73596fc Mon Sep 17 00:00:00 2001 From: Olivier Charvin Date: Mon, 18 Nov 2019 12:41:19 +0100 Subject: [PATCH 1/2] add panicing test on unsupported char --- fpdf_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/fpdf_test.go b/fpdf_test.go index 387f3a9c..7b8d5208 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -2833,6 +2833,25 @@ func TestIssue0316(t *testing.T) { } } +func TestMultiCellUnsupportedChar(t *testing.T) { + pdf := gofpdf.New("P", "mm", "A4", "") + pdf.AddPage() + fontBytes, _ := ioutil.ReadFile(example.FontFile("DejaVuSansCondensed.ttf")) + pdf.AddUTF8FontFromBytes("dejavu", "", fontBytes) + pdf.SetFont("dejavu", "", 16) + + defer func() { + if r := recover(); r != nil { + t.Errorf("unexpected panic: %v", r) + } + }() + + pdf.MultiCell(0, 5, "😀", "", "", false) + + fileStr := example.Filename("TestMultiCellUnsupportedChar") + pdf.OutputFileAndClose(fileStr) +} + // ExampleFpdf_SetTextRenderingMode demonstrates embedding files in PDFs, // at the top-level. func ExampleFpdf_SetAttachments() { From c4685a7728b6292626d70215c8d5e9c28aefe075 Mon Sep 17 00:00:00 2001 From: Olivier Charvin Date: Mon, 18 Nov 2019 12:43:46 +0100 Subject: [PATCH 2/2] prevent panic --- fpdf.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fpdf.go b/fpdf.go index 2d910583..bfcb46fe 100644 --- a/fpdf.go +++ b/fpdf.go @@ -2707,6 +2707,10 @@ func (f *Fpdf) MultiCell(w, h float64, txtStr, borderStr, alignStr string, fill ls = l ns++ } + if int(c) >= len(cw) { + f.err = fmt.Errorf("character outside the supported range: %s", string(c)) + return + } if cw[int(c)] == 0 { //Marker width 0 used for missing symbols l += f.currentFont.Desc.MissingWidth } else if cw[int(c)] != 65535 { //Marker width 65535 used for zero width symbols