fixes a bug in md4c where indentation would include nul bytes. Closes #5

This commit is contained in:
Rasmus Andersson 2020-10-15 11:52:28 -07:00
parent ded168b868
commit 183280de81
3 changed files with 103 additions and 3 deletions

View file

@ -26,7 +26,6 @@
#include "md4c.h"
#include <limits.h>
// #include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -4558,9 +4557,9 @@ md_process_verbatim_block_contents(MD_CTX* ctx, MD_TEXTTYPE text_type, const MD_
MD_ASSERT(indent >= 0);
/* Output code indentation. */
while(indent > (int) SIZEOF_ARRAY(indent_chunk_str)) {
while(indent >= indent_chunk_size) {
MD_TEXT(text_type, indent_chunk_str, indent_chunk_size);
indent -= SIZEOF_ARRAY(indent_chunk_str);
indent -= indent_chunk_size;
}
if(indent > 0)
MD_TEXT(text_type, indent_chunk_str, indent);

96
test/issue5.js Normal file
View file

@ -0,0 +1,96 @@
// https://github.com/rsms/markdown-wasm/issues/5
const libdir = process.argv.includes("-debug") ? "build/debug" : "dist"
const md = require(`../${libdir}/markdown.node.js`)
// this triggers the bug
const input1 = Buffer.from(
"```\n" +
" |\n" +
"```"
, "utf8")
const expected1 = Buffer.from(
"<pre><code>" +
" |\n" +
"</code></pre>\n"
, "utf8")
// this does not (indent is 1 less space)
const input2 = Buffer.from(
"```\n" +
" |\n" +
"```"
, "utf8")
const expected2 = Buffer.from(
"<pre><code>" +
" |\n" +
"</code></pre>\n"
, "utf8")
// this does not (indent is 1 more space)
const input3 = Buffer.from(
"```\n" +
" |\n" +
"```"
, "utf8")
const expected3 = Buffer.from(
"<pre><code>" +
" |\n" +
"</code></pre>\n"
, "utf8")
let fails = 0
check("test1", expected1, input1)
check("test2", expected2, input2)
check("test3", expected3, input3)
function check(name, expected, inputData) {
const actual = Buffer.from(md.parse(inputData, { asMemoryView: true }))
if (expected.compare(actual) == 0) {
console.log(`${name} OK`)
return
}
fails++
const line = "——————————————————————————————————————————————————"
const wave = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
console.error(`${name} FAIL.`)
console.error(`\n\nExpected output:\n${line}`)
inspectBuf(expected, actual)
console.error(`${line}\n\nActual output:\n${line}`)
inspectBuf(actual, expected)
console.error(line)
function inspectBuf(buf, otherbuf) {
process.stderr.write(buf)
if (buf[buf.length-1] != 0x0a) {
process.stderr.write("<no-ending-line-break>\n")
}
console.error(wave)
const styleReset = "\x1b[22;39m"
const styleNone = s=>s
const styleDiff = process.stderr.isTTY ? s => "\x1b[1;33m"+s+styleReset : styleNone
const styleErr = process.stderr.isTTY ? s => "\x1b[1;31m"+s+styleReset : styleNone
for (let i = 0; i < buf.length; i++) {
let b = buf[i]
let style = styleNone
if (b < 0x20 && b != 0x09 && b != 0x0A && b != 0x0D) {
// byte is unexpected control character (except TAB, CR, LF)
style = styleErr
} else if (otherbuf && otherbuf[i] != b) {
style = styleDiff
}
process.stderr.write(style(b.toString(16).padStart(2, '0')) + " ")
if (b == 0x0a) {
process.stderr.write("\n")
}
}
}
}
process.exit(fails > 0 ? 1 : 0)

5
test/test.sh Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
node issue5.js "$@"