From df6a8bc26c2184c6e05e7d8e907bfe0dfe00e0ac Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Apr 2021 23:25:32 +0200 Subject: [PATCH 1/3] Fix go-fuzz followup of https://github.com/go-gitea/gitea/pull/15175 --- tools/fuzz.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tools/fuzz.go b/tools/fuzz.go index 6ed1b40003d8c..19c5cd60816e0 100644 --- a/tools/fuzz.go +++ b/tools/fuzz.go @@ -7,6 +7,9 @@ package fuzz import ( + "bytes" + "io" + "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" ) @@ -18,17 +21,24 @@ import ( // (for example, the input is lexically correct and was parsed successfully). // -1 if the input must not be added to corpus even if gives new coverage and 0 otherwise. +var ( + renderContext = markup.RenderContext{ + URLPrefix: "https://example.com", + Metas: map[string]string{ + "user": "go-gitea", + "repo": "gitea", + }, + } + discardWriter = io.Discard +) + func FuzzMarkdownRenderRaw(data []byte) int { - _ = markdown.RenderRaw(data, "", false) + _ = markdown.RenderRaw(&renderContext, bytes.NewReader(data), discardWriter) return 1 } func FuzzMarkupPostProcess(data []byte) int { - var localMetas = map[string]string{ - "user": "go-gitea", - "repo": "gitea", - } - _, err := markup.PostProcess(data, "https://example.com", localMetas, false) + err := markup.PostProcess(&renderContext, bytes.NewReader(data), discardWriter) if err != nil { return 0 } From b5410c1a0678152fcf9e8777d5f4d8c9309d5f31 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Apr 2021 23:27:20 +0200 Subject: [PATCH 2/3] simplify --- tools/fuzz.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/fuzz.go b/tools/fuzz.go index 19c5cd60816e0..761b7f1778d28 100644 --- a/tools/fuzz.go +++ b/tools/fuzz.go @@ -29,16 +29,15 @@ var ( "repo": "gitea", }, } - discardWriter = io.Discard ) func FuzzMarkdownRenderRaw(data []byte) int { - _ = markdown.RenderRaw(&renderContext, bytes.NewReader(data), discardWriter) + _ = markdown.RenderRaw(&renderContext, bytes.NewReader(data), io.Discard) return 1 } func FuzzMarkupPostProcess(data []byte) int { - err := markup.PostProcess(&renderContext, bytes.NewReader(data), discardWriter) + err := markup.PostProcess(&renderContext, bytes.NewReader(data), io.Discard) if err != nil { return 0 } From 18e488944012de031ef76edbfb04618949325da6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Apr 2021 23:29:16 +0200 Subject: [PATCH 3/3] enhance --- tools/fuzz.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/fuzz.go b/tools/fuzz.go index 761b7f1778d28..4b5b72d1d0d52 100644 --- a/tools/fuzz.go +++ b/tools/fuzz.go @@ -32,7 +32,10 @@ var ( ) func FuzzMarkdownRenderRaw(data []byte) int { - _ = markdown.RenderRaw(&renderContext, bytes.NewReader(data), io.Discard) + err := markdown.RenderRaw(&renderContext, bytes.NewReader(data), io.Discard) + if err != nil { + return 0 + } return 1 }