Skip to content

write() error check inverted in profile exporter #498

@xroche

Description

@xroche

Description

write_profile() in src/exporter/ddprof_exporter.cc (line 62-68) checks the wrong return value from write():

DDRes write_profile(const ddog_ByteSlice *buffer, int fd) {
  if (write(fd, buffer->ptr, buffer->len) == 0) {  // BUG: should check == -1
    DDRES_RETURN_ERROR_LOG(DD_WHAT_EXPORTER,
                           "Failed to write byte buffer to stdout! %s\n",
                           strerror(errno));
  }
  return {};
}

write() returns -1 on error, not 0. The current check:

  • Misses all real errors (ENOSPC, EIO, EPIPE, etc.) — returns success when the write actually failed
  • False-alarms on legitimate 0-byte writes (empty buffer) — returns error when nothing is wrong
  • Doesn't handle short writeswrite() may return less than buffer->len

Impact

Profile data is silently lost on disk errors. This causes invisible profiling gaps with no diagnostic signal, making it difficult to debug missing profiles in production.

Fix

Check for == -1 (or < 0) instead of == 0. Ideally also handle short writes with a loop.

Classification

  • CWE-253: Incorrect Check of Function Return Value

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions