gofpdf icon indicating copy to clipboard operation
gofpdf copied to clipboard

[PANIC] Index out of range

Open IGLOU-EU opened this issue 1 year ago • 1 comments

Hi, I have an index out of range with [email protected]

panic: runtime error: index out of range [53] with length 0
goroutine 66 [running]:
github.com/phpdave11/gofpdf.(*Fpdf).write(0xc0003ac000, 0x4020000000000000, {0xc0004eaf00?, 0x4?}, 0x4?, {0x0, 0x0})
         /root/go/pkg/mod/github.com/phpdave11/[email protected]/fpdf.go:2839 +0xbea
github.com/phpdave11/gofpdf.(*Fpdf).Write(...)
         /root/go/pkg/mod/github.com/phpdave11/[email protected]/fpdf.go:2901
[...]

There is happening after a call to Write. Used font are UbuntuFont

strBuff = fmt.Sprintf(
    "\n%s, %s, %s %s\n", 
    data.Simple["se-addr-num"], 
    data.Simple["se-addr-complement"], 
    data.Simple["se-addr-code"], 
    data.Simple["se-addr-commune"],
)

log.Println("DEBUG PANIC: ", strBuff)
pdf.Write(8, strBuff)

:thinking:

IGLOU-EU avatar Mar 03 '23 15:03 IGLOU-EU

This happens when we try to write without a loaded font.

My font isn't loaded because I haven't called "SetFontLocation", I'm pointing directly to the root path. This problem happens here: https://github.com/phpdave11/gofpdf/blob/b09d9214a2296c8ea10312d9c5a520e27f2148c9/fpdf.go#L74 https://github.com/phpdave11/gofpdf/blob/b09d9214a2296c8ea10312d9c5a520e27f2148c9/fpdf.go#L114 https://github.com/phpdave11/gofpdf/blob/b09d9214a2296c8ea10312d9c5a520e27f2148c9/fpdf.go#L1687

The behavior of path.Join' used with '.' is to remove the '/' if the target has one. Ex path.Join(".", "/tata/titi/toto.ttf")` = "tata/titi/toto.ttf"

To solve this issue:

  • Change the magic value of fontDirStr
  • Check if a font is loaded instead of panic

I planned to make a PR, what do you think about?

IGLOU-EU avatar Jun 19 '23 18:06 IGLOU-EU