diff --git a/src/sourcemap/sourcemap.zig b/src/sourcemap/sourcemap.zig index 8804bd9d4d..be4eca94e6 100644 --- a/src/sourcemap/sourcemap.zig +++ b/src/sourcemap/sourcemap.zig @@ -449,6 +449,16 @@ pub const LineColumnOffset = struct { var iter = strings.CodepointIterator.initOffset(input, i); var cursor = strings.CodepointIterator.Cursor{ .i = @as(u32, @truncate(iter.i)) }; _ = iter.next(&cursor); + + // Given a null byte, cursor.width becomes 0 + // This can lead to integer overflow, crashes, or hangs. + // https://github.com/oven-sh/bun/issues/10624 + if (cursor.width == 0) { + columns += 1; + offset = i + 1; + continue; + } + offset = i + cursor.width; switch (cursor.c) { diff --git a/test/js/third_party/body-parser/express-bun-build-compile.test.ts b/test/js/third_party/body-parser/express-bun-build-compile.test.ts new file mode 100644 index 0000000000..b9684a033b --- /dev/null +++ b/test/js/third_party/body-parser/express-bun-build-compile.test.ts @@ -0,0 +1,18 @@ +import { expect, test } from "bun:test"; +import { join } from "path"; +import { $ } from "bun"; +import "harness"; +import { bunExe, tempDirWithFiles } from "harness"; + +$.throws(true); + +// https://github.com/oven-sh/bun/issues/10624 +test("Express hello world app supports bun build --compile --minify --sourcemap", async () => { + const dir = tempDirWithFiles("express-bun-build-compile", { + "out.exe": "", + }); + + const file = join(dir, "out.exe"); + await $`${bunExe()} build --compile --minify --sourcemap ${join(import.meta.dir, "express-compile-fixture.ts")} --outfile=${file}`; + await $`${file}`; +}); diff --git a/test/js/third_party/body-parser/express-compile-fixture.ts b/test/js/third_party/body-parser/express-compile-fixture.ts new file mode 100644 index 0000000000..c74234b2a9 --- /dev/null +++ b/test/js/third_party/body-parser/express-compile-fixture.ts @@ -0,0 +1,20 @@ +const express = require("express"); +const app = express(); +const port = 0; + +app.get("/", (req, res) => { + res.send("Hello World!"); +}); + +const server = app.listen(port, () => { + fetch(`http://localhost:${server.address().port}`).then(res => { + res.text().then(text => { + if (text !== "Hello World!") { + console.error("Expected 'Hello World!', got", text); + process.exit(1); + } + console.log("OK"); + process.exit(0); + }); + }); +});