[emstar-design] bug report: libmisc file_to_buf leaks file descriptors

William Wright billwright1234 at gmail.com
Wed Jun 8 05:16:59 PDT 2005


Hi,

The file_to_buf function in misc_buf.c only closes its file on
abnormal exit.  I suggest changing it to something like this:

int file_to_buf(buf_t *buf, const char *filename)
{
  int status;
  char rd_buf[4096];

  int fd = open(filename, O_RDONLY);
  if (fd < 0) return -1;

  while (1) {
    status = read(fd, rd_buf, sizeof(rd_buf));
    if (status < 0) {
      close(fd);
      return -1;
    }
    if (status == 0) {
      close(fd);  /* close on normal exit */
      return buf->len;
    }
    bufcpy(buf, rd_buf, status);
  }
}


Thanks,
Bill Wright



More information about the emstar-design mailing list