Skip to content

fix(csv-stringify): reject null records instead of crashing - #570

Open
bardrop wants to merge 1 commit into
adaltas:masterfrom
bardrop-oss:fix/stringify-null-record
Open

bardrop wants to merge 1 commit into
adaltas:masterfrom
bardrop-oss:fix/stringify-null-record

Conversation

@bardrop

@bardrop bardrop commented Nov 17, 2026

Copy link
Copy Markdown

A null record slips past the "Invalid Record" check in __transform, because typeof null is "object". What happens next depends on the API:

import { stringify } from "csv-stringify/sync";
stringify([null]);            // TypeError: Cannot convert undefined or null to object
stringify([{ a: "1" }, null]); // "a\n1\n\n" — silently writes an empty row
import { stringify } from "csv-stringify";
stringify([{ a: "1" }, null], (err, data) => {});
// Uncaught TypeError [ERR_STREAM_NULL_VALUES]: May not write null values to stream

The callback case is the worst one: writer() runs in setImmediate, so stringifier.write(null) throws outside the caller's reach, the process crashes and the callback is never called. Other bad records (undefined, 1, true) already reach the callback as an Invalid Record error.

Changes:

  • __transform now rejects null with Invalid Record: expect an array or an object, got null, the same error as other non-record values. The sync API throws it.
  • The callback/data API does not write null into the stream (streams reserve it for end of input). It destroys the stringifier with that same error, so the callback (or error listener) gets it.

Tests: 2 new cases in test/api.sync.js and 1 in test/api.callback.js. All 3 fail on master (wrong TypeError, no throw, uncaught exception) and pass with the fix. npm test in csv-stringify (246 passing) and the csv package tests pass; lint and tsc are clean. Generated dist is left to the release build.

A null record passed the 'Invalid Record' check because typeof null is
'object'. The sync API then threw a TypeError for a first null record and
wrote an empty row for a later one. The callback API wrote null to the
stream, which throws ERR_STREAM_NULL_VALUES inside setImmediate, so the
process crashed and the callback never ran.

Null records now fail with 'Invalid Record: expect an array or an
object, got null', thrown by the sync API and passed to the callback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant