Skip to content

panic: runtime error: invalid memory address or nil pointer dereference #4895

Open
@helperShang

Description

@helperShang

tinygo version

tinygo version 0.37.0 darwin/arm64 (using go version go1.24.3 and LLVM version 19.1.2)

source code

package main

import (
	"fmt"
	"io"

	"github.com/alecthomas/chroma/v2"
	"github.com/alecthomas/chroma/v2/formatters/html"
	"github.com/alecthomas/chroma/v2/lexers"
	"github.com/alecthomas/chroma/v2/styles"
	"github.com/gomarkdown/markdown"
	"github.com/gomarkdown/markdown/ast"
	mdhtml "github.com/gomarkdown/markdown/html"
)

func htmlHighlight(w io.Writer, source, lang, defaultLang string) error {
	if lang == "" {
		lang = defaultLang
	}
	l := lexers.Get(lang)
	if l == nil {
		l = lexers.Analyse(source)
	}
	if l == nil {
		l = lexers.Fallback
	}
	l = chroma.Coalesce(l)

	it, err := l.Tokenise(nil, source)
	if err != nil {
		return err
	}
	htmlFormatter := html.New(html.WithClasses(true), html.TabWidth(2))
	if htmlFormatter == nil {
		panic("couldn't create html formatter")
	}
	styleName := "monokailight"
	highlightStyle := styles.Get(styleName)
	if highlightStyle == nil {
		panic(fmt.Sprintf("didn't find style '%s'", styleName))
	}
	return htmlFormatter.Format(w, highlightStyle, it)
}

// an actual rendering of Paragraph is more complicated
func renderCode(w io.Writer, codeBlock *ast.CodeBlock, entering bool) {
	defaultLang := ""
	lang := string(codeBlock.Info)
	htmlHighlight(w, string(codeBlock.Literal), lang, defaultLang)
}

func myRenderHook(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
	if code, ok := node.(*ast.CodeBlock); ok {
		renderCode(w, code, entering)
		return ast.GoToNext, true
	}
	return ast.GoToNext, false
}

func newCustomizedRender() *mdhtml.Renderer {
	opts := mdhtml.RendererOptions{
		Flags:          mdhtml.CommonFlags,
		RenderNodeHook: myRenderHook,
	}
	return mdhtml.NewRenderer(opts)
}

func main() {
	markdown.ToHTML([]byte(`# asfasdfs`), nil, newCustomizedRender())
}

build

tinygo build -target wasm -x

out

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x38 pc=0x102432e30]

goroutine 241 [running]:
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a2870, {0x140088a8290, 0x6, 0x1400887fa28?}, 0x1400393ecf0, {0x1400846d2c0, 0x28})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:369 +0x3470
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a27d0, {0x1400889b390, 0x2, 0x1400887eb28?}, 0x140039113b0, {0x1400846d1d0, 0x24})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a2780, {0x1400888bc10, 0x3, 0x1400887e408?}, 0x1400367fb00, {0x140088969c0, 0x20})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a26e0, {0x1400887d790, 0x2, 0x14008665bf0?}, 0x140035d3410, {0x14008896400, 0x1c})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a2690, {0x14008804290, 0x2, 0x140086644f8?}, 0x14003580990, {0x14008833758, 0x18})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x140027a2500, {0x1400875ab10, 0x2, 0x14008746930?}, 0x14002a44600, {0x14008629800, 0x14})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x14003787180, {0x14008749310, 0x5, 0x10855fbc0?}, 0x140018d4780, {0x14008758c50, 0x10})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x14003787130, {0x14007d1ea10, 0x5, 0x10855fbc0?}, 0x140048671a0, {0x14008743090, 0xc})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x14003787040, {0x14007d1e810, 0x5, 0x10855fbc0?}, 0x14004df83f0, {0x1400873aa68, 0x8})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.(*runner).run(0x14006a6b4a0, 0x1400378b4a0, {0x0, 0x0, 0x140000839a8?}, 0x0, {0x106c550b3, 0x4})
        /Users/runner/work/tinygo/tinygo/interp/interpreter.go:560 +0x669c
github.com/tinygo-org/tinygo/interp.Run({0x14000083dd8?}, 0x1024974f8?, 0xf0?)
        /Users/runner/work/tinygo/tinygo/interp/interp.go:121 +0x558
github.com/tinygo-org/tinygo/builder.optimizeProgram({0x1180a3f60?}, 0x140002a4000)
        /Users/runner/work/tinygo/tinygo/builder/build.go:1180 +0x30
github.com/tinygo-org/tinygo/builder.Build.func5(0x14005e294a0?)
        /Users/runner/work/tinygo/tinygo/builder/build.go:611 +0x504
github.com/tinygo-org/tinygo/builder.runJob(0x14005e29500, 0x14000113b20)
        /Users/runner/work/tinygo/tinygo/builder/jobs.go:212 +0x48
created by github.com/tinygo-org/tinygo/builder.runJobs in goroutine 1
        /Users/runner/work/tinygo/tinygo/builder/jobs.go:113 +0x45c

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingnext-releaseWill be part of next release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions