Skip to content

Commit 6b7b573

Browse files
authored
libreprl: rename stdout and stderr fields (NFC) (googleprojectzero#273)
This renames the `stdout` and `stderr` fields to `child_stdout` and `child_strerr` respectively. `stdout` and `stderr` are reserved by the C standard library, which may define them using a macro. In such a case, the re-use of the identifier literal as a field will be improperly expanded by the CPP. This actually occurs in practive with ucrt, which uses macros for the indirection of the iostreams. This makes the code more resilient to different libc implementations.
1 parent f188937 commit 6b7b573

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

Sources/libreprl/libreprl.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ struct reprl_context {
9494
struct data_channel* data_out;
9595

9696
// Optional data channel for the child's stdout and stderr.
97-
struct data_channel* stdout;
98-
struct data_channel* stderr;
97+
struct data_channel* child_stdout;
98+
struct data_channel* child_stderr;
9999

100100
// PID of the child process. Will be zero if no child process is currently running.
101101
int pid;
@@ -176,8 +176,8 @@ static int reprl_spawn_child(struct reprl_context* ctx)
176176
// This is also a good time to ensure the data channel backing files don't grow too large.
177177
ftruncate(ctx->data_in->fd, REPRL_MAX_DATA_SIZE);
178178
ftruncate(ctx->data_out->fd, REPRL_MAX_DATA_SIZE);
179-
if (ctx->stdout) ftruncate(ctx->stdout->fd, REPRL_MAX_DATA_SIZE);
180-
if (ctx->stderr) ftruncate(ctx->stderr->fd, REPRL_MAX_DATA_SIZE);
179+
if (ctx->child_stdout) ftruncate(ctx->child_stdout->fd, REPRL_MAX_DATA_SIZE);
180+
if (ctx->child_stderr) ftruncate(ctx->child_stderr->fd, REPRL_MAX_DATA_SIZE);
181181

182182
int crpipe[2] = { 0, 0 }; // control pipe child -> reprl
183183
int cwpipe[2] = { 0, 0 }; // control pipe reprl -> child
@@ -225,9 +225,9 @@ static int reprl_spawn_child(struct reprl_context* ctx)
225225

226226
int devnull = open("/dev/null", O_RDWR);
227227
dup2(devnull, 0);
228-
if (ctx->stdout) dup2(ctx->stdout->fd, 1);
228+
if (ctx->child_stdout) dup2(ctx->child_stdout->fd, 1);
229229
else dup2(devnull, 1);
230-
if (ctx->stderr) dup2(ctx->stderr->fd, 2);
230+
if (ctx->child_stderr) dup2(ctx->child_stderr->fd, 2);
231231
else dup2(devnull, 2);
232232
close(devnull);
233233

@@ -307,12 +307,12 @@ int reprl_initialize_context(struct reprl_context* ctx, const char** argv, const
307307
ctx->data_in = reprl_create_data_channel(ctx);
308308
ctx->data_out = reprl_create_data_channel(ctx);
309309
if (capture_stdout) {
310-
ctx->stdout = reprl_create_data_channel(ctx);
310+
ctx->child_stdout = reprl_create_data_channel(ctx);
311311
}
312312
if (capture_stderr) {
313-
ctx->stderr = reprl_create_data_channel(ctx);
313+
ctx->child_stderr = reprl_create_data_channel(ctx);
314314
}
315-
if (!ctx->data_in || !ctx->data_out || (capture_stdout && !ctx->stdout) || (capture_stderr && !ctx->stderr)) {
315+
if (!ctx->data_in || !ctx->data_out || (capture_stdout && !ctx->child_stdout) || (capture_stderr && !ctx->child_stderr)) {
316316
// Proper error message will have been set by reprl_create_data_channel
317317
return -1;
318318
}
@@ -330,8 +330,8 @@ void reprl_destroy_context(struct reprl_context* ctx)
330330

331331
reprl_destroy_data_channel(ctx, ctx->data_in);
332332
reprl_destroy_data_channel(ctx, ctx->data_out);
333-
reprl_destroy_data_channel(ctx, ctx->stdout);
334-
reprl_destroy_data_channel(ctx, ctx->stderr);
333+
reprl_destroy_data_channel(ctx, ctx->child_stdout);
334+
reprl_destroy_data_channel(ctx, ctx->child_stderr);
335335

336336
free(ctx->last_error);
337337
free(ctx);
@@ -354,11 +354,11 @@ int reprl_execute(struct reprl_context* ctx, const char* script, uint64_t script
354354
// Reset file position so the child can simply read(2) and write(2) to these fds.
355355
lseek(ctx->data_out->fd, 0, SEEK_SET);
356356
lseek(ctx->data_in->fd, 0, SEEK_SET);
357-
if (ctx->stdout) {
358-
lseek(ctx->stdout->fd, 0, SEEK_SET);
357+
if (ctx->child_stdout) {
358+
lseek(ctx->child_stdout->fd, 0, SEEK_SET);
359359
}
360-
if (ctx->stderr) {
361-
lseek(ctx->stderr->fd, 0, SEEK_SET);
360+
if (ctx->child_stderr) {
361+
lseek(ctx->child_stderr->fd, 0, SEEK_SET);
362362
}
363363

364364
// Spawn a new instance if necessary.
@@ -490,12 +490,12 @@ const char* reprl_fetch_fuzzout(struct reprl_context* ctx)
490490

491491
const char* reprl_fetch_stdout(struct reprl_context* ctx)
492492
{
493-
return fetch_data_channel_content(ctx->stdout);
493+
return fetch_data_channel_content(ctx->child_stdout);
494494
}
495495

496496
const char* reprl_fetch_stderr(struct reprl_context* ctx)
497497
{
498-
return fetch_data_channel_content(ctx->stderr);
498+
return fetch_data_channel_content(ctx->child_stderr);
499499
}
500500

501501
const char* reprl_get_last_error(struct reprl_context* ctx)

0 commit comments

Comments
 (0)