[io] pcl::io::raw_open opens files in text mode on Windows, so raw_read alters binary data

Open Beginner friendly
#6,536 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp

Research direction

Start in pcl/io/low_level_io.h, in the _WIN32 branch where raw_open wraps ::_open; compare those overloads with the POSIX ones. Reproduce with a tiny binary file containing 0D 0A and 1A, as in the issue, and check raw_read byte counts. Then exercise the affected callers PCDReader::read for binary_compressed files and LineRGBD::loadTemplates. Done means Windows raw_read returns the file bytes unchanged and no longer truncates at CRLF or 0x1A.

Written by the indexing model from the issue text.

Description

kind: bug status: triage

Describe the bug

On Windows, pcl::io::raw_open (pcl/io/low_level_io.h) calls ::_open without _O_BINARY. The CRT default translation mode is text (_O_TEXT, unless the application changes _fmode). As a result, pcl::io::raw_read on that descriptor:

  • collapses every CR LF pair into LF, compacting the buffer in place, and returns fewer bytes;
  • stops at a 0x1A byte.

POSIX open has no text mode, so the same code reads different bytes on Linux and Windows.

PCL reads binary data through these wrappers: PCDReader::read for binary_compressed files and LineRGBD::loadTemplates. raw_open/raw_read are also public API for user code.

Expected behavior

raw_read returns the file's bytes unchanged, as it does on POSIX.

Current Behavior

A file holds the bytes 0D 0A 00 01 40 00 00 02, and 4 bytes are read from it:

| open flags | _read returns | buffer
| O_RDONLY (what raw_open does) | 3 | 0A 00 01 01
| O_RDONLY \| _O_BINARY | 4 | 0D 0A 00 01

With the bytes 1A 0A 00 00, text mode returns 0.

Consequences:

  • LineRGBD::loadTemplates reads each .sqmmt entry with raw_read (ltm_fd, buffer, fsize) and checks only for -1. After the first 0D 0A pair, every following byte is shifted by one and the tail holds stale bytes. sqmmt.deserialize then reads corrupted data.
  • PCDReader::read reads the 4-byte compressed size of a binary_compressed file with raw_read and checks only num_read < 0. On Windows the value is used only for the file-size check, because the whole file is mapped. When translation makes the value larger, a valid file is rejected with "Corrupted PCD file. The file is smaller than expected!". For example, a compressed size of 0x01000A0D (16 779 789 bytes) is read as 0x0101000A, which is 62 973 bytes too large.

To Reproduce

#include <pcl/io/low_level_io.h>
#include <fcntl.h>
#include <cstdio>
#include <fstream>

int main ()
{
  const unsigned char bytes[] = {0x0D, 0x0A, 0x00, 0x01};
  std::ofstream ("crlf.bin", std::ios::binary).write (reinterpret_cast<const char*> (bytes), sizeof (bytes));

  int fd = pcl::io::raw_open ("crlf.bin", O_RDONLY);
  unsigned char buf[4] = {};
  const auto n = pcl::io::raw_read (fd, buf,
  pcl::io::raw_close (fd);
  std::printf ("n=%d %02X %02X %02X %02X\n",, buf[1], buf[2], buf[3]);
  // Linux:   n=4 0D 0A 00 01
  // Windows: n=3 0A 00 01 01
}

Your Environment (please complete the following information):

  • OS: Windows 11
  • Compiler: MSVC (Visual Studio 2026)
  • PCL Version: 1.12.1 and current master

Possible Solution

Open in binary mode inside the Windows wrappers. O_BINARY does not exist on POSIX, so callers cannot pass it portably.

#ifdef _WIN32
# include <fcntl.h> // _O_BINARY
...
    inline int raw_open(const char * pathname, int flags, int mode)
    {
      return ::_open(pathname, flags | _O_BINARY, mode);
    }

    inline int raw_open(const char * pathname, int flags)
    {
      return ::_open(pathname, flags | _O_BINARY);
    }
Dominant language
C++
Stars
11.2k
Forks
4.7k
Avg merge
6d 13h
Merged PRs (30d)
10

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PointCloudLibrary/pcl

All issues in PointCloudLibrary/pcl

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.