From 4a46613916cdfa6a168746aba6abcd698cd17875 Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Mon, 15 Aug 2016 15:27:19 -0700
Subject: markdown: fix treating pure number as SHA1

- Detect non-exist commit and return 404 not 500
---
 modules/markdown/markdown.go | 5 +++++
 1 file changed, 5 insertions(+)

(limited to 'modules/markdown')

diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go
index a6cdf222..7bfb0ec6 100644
--- a/modules/markdown/markdown.go
+++ b/modules/markdown/markdown.go
@@ -91,6 +91,8 @@ var (
 	IssueAlphanumericPattern = regexp.MustCompile(`( |^|\()[A-Z]{1,10}-[1-9][0-9]*\b`)
 
 	// Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae
+	// FIXME: this pattern matches pure numbers as well, right now we do a hack to check in RenderSha1CurrentPattern
+	// by converting string to a number.
 	Sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{7,40}\b`)
 )
 
@@ -262,6 +264,9 @@ func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string
 // RenderSha1CurrentPattern renders SHA1 strings to corresponding links that assumes in the same repository.
 func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
 	return []byte(Sha1CurrentPattern.ReplaceAllStringFunc(string(rawBytes[:]), func(m string) string {
+		if com.StrTo(m).MustInt() > 0 {
+			return m
+		}
 		return fmt.Sprintf(`<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, base.ShortSha(string(m)))
 	}))
 }
-- 
cgit v1.2.3