Skip to content

Commit e4dc332

Browse files
committed
io: Initialize pipe file descriptors to -1.
If we abort setup due to I/O initialization failure, we call PIPE_CLOSE which will close the pipes if the file descriptors are not -1. However, we were not initializing the file descriptors to -1, which left them at 0 after calloc(), leading us to attempt to close fd 0. We now initialize all the pipe file descriptors to -1 properly in all I/O modules. LBBS-142 #close
1 parent ca38513 commit e4dc332

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

io/io_compress.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ static int setup(int *rfd, int *wfd, enum bbs_io_transform_dir dir, void **restr
214214
return -1;
215215
}
216216

217+
/* In case we abort, make sure the file descriptors are valid */
218+
z->rpfd[0] = z->rpfd[1] = -1;
219+
z->wpfd[0] = z->wpfd[1] = -1;
220+
217221
z->compressor = &z->compressor_s;
218222
z->decompressor = &z->decompressor_s;
219223
z->level = DEFAULT_COMPRESSION_LEVEL;

io/io_log.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ static int setup(int *rfd, int *wfd, enum bbs_io_transform_dir dir, void **restr
186186
return -1;
187187
}
188188

189+
/* In case we abort, make sure the file descriptors are valid */
190+
l->rpfd[0] = l->rpfd[1] = -1;
191+
l->wpfd[0] = l->wpfd[1] = -1;
192+
189193
l->fp = bbs_mkftemp(template, 0600);
190194
if (!l->fp) {
191195
goto fail;

io/io_tls.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ static struct tls_client *ssl_launch(SSL *ssl, int fd, int *rfd, int *wfd, int c
302302
if (ALLOC_FAILURE(t)) {
303303
return NULL;
304304
}
305+
306+
/* In case we abort, make sure the file descriptors are valid */
307+
t->readpipe[0] = t->readpipe[1] = -1;
308+
t->writepipe[0] = t->writepipe[1] = -1;
309+
305310
t->ssl = ssl;
306311
t->fd = fd;
307312
if (pipe(t->readpipe)) {

0 commit comments

Comments
 (0)