# SVG 上传时标题注释为 3 个字符失败，2 个字符可成功

**URL:** <https://meta.discourse.org/t/svg-uplaod-with-title-comment-of-3-characters-fails-two-characters-work/120155>\
**Category:** Bug\
**Created:** [2019年六月12日 14:46 UTC](https://meta.discourse.org/t/svg-uplaod-with-title-comment-of-3-characters-fails-two-characters-work/120155 "2019-06-12T14:46:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://meta.discourse.org/u/EricGT)\
**Post date:** [2019年六月12日 14:46 UTC](https://meta.discourse.org/t/svg-uplaod-with-title-comment-of-3-characters-fails-two-characters-work/120155/1 "2019-06-12T14:46:44Z")

</div>

Uploading an SVG that contains

```plaintext
<!-- Title: ABC Pages: 1 -->

```

fails with

> Sorry, but we couldn’t determine the size of the image. Maybe your image is corrupted?

Changing title to

```plaintext
<!-- Title: AB Pages: 1 -->

```

works. Yes, just changing `ABC` to `AB` made the difference.

It appears the length of the title name in the comment is limited to just two characters. Maybe something else but this is the case that reproduces the error every time.

i.e.

If a user sees the error message:

> Sorry, but we couldn’t determine the size of the image. Maybe your image is corrupted?

how is a user upon seeing the error message to know that one of the [possible](https://meta.discourse.org/t/svg-upload-with-cr-lf-fails-change-to-lf-works/120148) problems is that they should change the length of the title in the comment to two or less characters.

or another option is that

discourse should modify the security scanner/whistleblower to allow title comments of other lengths valid. 😃

* * *

The following SVG have line ends of LF and not [CR/LF](https://meta.discourse.org/t/svg-upload-with-cr-lf-fails-change-to-lf-works/120148)

Failing SVG

```xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.38.0 (20140413.2041)
 -->
<!-- Title: ABC Pages: 1 -->
<svg width="110pt" height="64pt"
 viewBox="0.00 0.00 110.00 64.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 60)">
<title>Test</title>
<polygon fill="black" stroke="none" points="-4,4 -4,-60 106,-60 106,4 -4,4"/>
</g>
</svg>

```

Working SVG

```xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.38.0 (20140413.2041)
 -->
<!-- Title: AB Pages: 1 -->
<svg width="110pt" height="64pt"
 viewBox="0.00 0.00 110.00 64.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 60)">
<title>Test</title>
<polygon fill="black" stroke="none" points="-4,4 -4,-60 106,-60 106,4 -4,4"/>
</g>
</svg>

```

---

<div class="post-metadata">

**Author:** ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)\
**Post date:** [2019年六月12日 15:11 UTC](https://meta.discourse.org/t/svg-uplaod-with-title-comment-of-3-characters-fails-two-characters-work/120155/2 "2019-06-12T15:11:11Z")

</div>

And if you add the extra character back it begins to fail again?

The are just xml comments, aren’t they? Is there any real impact in removing them?

---

<div class="post-metadata">

**Author:** ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)\
**Post date:** [2019年六月12日 15:21 UTC](https://meta.discourse.org/t/svg-uplaod-with-title-comment-of-3-characters-fails-two-characters-work/120155/3 "2019-06-12T15:21:05Z")

</div>

Very strange… it seems to have something to do with the length of the comments?

If I remove any 1 character from a commented section it works (not just `<!-- Title: ABC Pages: 1 -->` but also the `<!-- Generated by graphviz version 2.38.0 (20140413.2041) -->`).

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://meta.discourse.org/u/EricGT)\
**Post date:** [2019年六月12日 15:21 UTC](https://meta.discourse.org/t/svg-uplaod-with-title-comment-of-3-characters-fails-two-characters-work/120155/4 "2019-06-12T15:21:39Z")

</div>

> [@Stephen](#):
>
> And if you add the extra character back it begins to fail again?

Yes.

Taking a file that successfully uploads with 2 characters and adding the third character causes the upload to fail.

The reverse is also true.

Taking a file that fails to upload with 3 characters and removing the third character causes the upload to succeed.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://meta.discourse.org/u/EricGT)\
**Post date:** [2019年六月12日 16:19 UTC](https://meta.discourse.org/t/svg-uplaod-with-title-comment-of-3-characters-fails-two-characters-work/120155/6 "2019-06-12T16:19:40Z")

</div>

> [@Stephen](#):
>
> The are just xml comments, aren’t they? Is there any real impact in removing them?

No there is technically no impact in removing them from the file.

However, the use-case in which these are being generated and used at a Discourse site is that

1. [We](https://swi-prolog.discourse.group/) have [Wiki post](https://meta.discourse.org/t/what-is-a-wiki-post/30801) (Discourse post changed to Wiki so that any Basic user can edit them) used as Wiki pages (pages that are meant to be a source of common input and general knowledge; think [Wikipedia](https://www.wikipedia.org/)). Thus one word with two different meanings here.

2. I frequently generate content using [Graphviz](https://www.graphviz.org/) - [DOT](https://graphviz.gitlab.io/_pages/doc/info/lang.html) and export it to SVG for use with such Wiki pages.

While I can program with DOT, SVG, and HTML, some to many users are not as skilled. The [site](https://swi-prolog.discourse.group/) is for SWI-Prolog and some of the users are high school/college students just learning programming. While us advanced programmers would consider learning any of these as just learning another programming language and just get it done, the site is to help others learn Prolog. I can’t expect them to edit DOT, SVG, and much less HTML.

1. The pages being Wiki pages for editing by and for everyone, expecting them to also figure out that once they create an SVG they have to convert the CR/LF to LF and then find and remove the offending comment line is a bit to much to ask. My goal is to get them to help create useful Wiki pages, not pull their hair out.

Anyway, the bug is now noted and ways to work around it for experienced programmers is demonstrated either by changing the text or removing the offending comment.

Thanks. 😃

PS

If the offending comment is a copyright, CC or other license, then that would be a problem. Also while I don’t know of or use any such add-on, add-ons sometimes extend languages by using specific patterns in comments as part of a language and removing them would be damaging.

---

<div class="post-metadata">

**Author:** ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)\
**Post date:** [2019年六月28日 19:04 UTC](https://meta.discourse.org/t/svg-uplaod-with-title-comment-of-3-characters-fails-two-characters-work/120155/7 "2019-06-28T19:04:07Z")

</div>

这是由于我们使用的 [fastimage](https://github.com/sdsykes/fastimage) gem 中的以下代码导致的：

> <https://github.com/sdsykes/fastimage/blob/18831de7de75fb069b048aa9fcabc1f8b76efcad/lib/fastimage.rb#L506-L515>

该代码会在文件的前 **250 个字符** 中查找“`<svg`”字符串（依个人之见，这已经绰绰有余）。

如果 graphviz 能生成更干净的 SVG 文件就太好了……
