镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

fs.write does not work on external two-byte strings #18146

Description

@joyeecheung
  • Version: master
  • Platform: all
  • Subsystem: fs

Discovered during #18144

// Flags: --expose_externalize_string

'use strict';

const assert = require('assert');
const fs = require('fs');
const file = 'write-external.txt';
const expected = '中文';
externalizeString(expected);

fs.open(file, 'w', 0o644, function(err, fd) {
  fs.write(fd, expected, 0, 'utf8', function(err, written) {
    fs.closeSync(fd);
    const found = fs.readFileSync(file, 'utf8');
    fs.unlinkSync(file);
    assert.strictEqual(expected, found);  // AssertionError [ERR_ASSERTION]: '中文' strictEqual '-N�e'
  });
});

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jan 14, 2018
  2. targos commented on Jan 15, 2018

    @targos
    Member

    /cc @nodejs/v8

  3. bnoordhuis commented on Jan 17, 2018

    @bnoordhuis
    Member

    The surface issue is that fs.write() ignores the encoding argument with externalized strings. The broader issue is that the way node handles external strings is broken and unsafe.

    Broken because string_bytes.cc ignores the encoding. Unsafe because the string is collectible by the GC. Storing the pointer like async fs.write() does can result in segfaults and use-after-frees.

    I'll see if I can put together a pull request.

  4. bnoordhuis commented on Jan 17, 2018

    @bnoordhuis
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions