Skip to content

Commit aa90e14

Browse files
committed
Suppress -Wunused-result warnings about write() and fwrite().
This is merely an exercise in satisfying pedants, not a bug fix, because in every case we were checking for failure later with ferror(), or else there was nothing useful to be done about a failure anyway. Document the latter cases.
1 parent c53d3a9 commit aa90e14

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

src/backend/access/transam/xlog.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9067,8 +9067,10 @@ do_pg_start_backup(const char *backupidstr, bool fast, char **labelfile)
90679067
(errcode_for_file_access(),
90689068
errmsg("could not create file \"%s\": %m",
90699069
BACKUP_LABEL_FILE)));
9070-
fwrite(labelfbuf.data, labelfbuf.len, 1, fp);
9071-
if (fflush(fp) || ferror(fp) || FreeFile(fp))
9070+
if (fwrite(labelfbuf.data, labelfbuf.len, 1, fp) != 1 ||
9071+
fflush(fp) != 0 ||
9072+
ferror(fp) ||
9073+
FreeFile(fp))
90729074
ereport(ERROR,
90739075
(errcode_for_file_access(),
90749076
errmsg("could not write file \"%s\": %m",

src/backend/commands/copy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ CopySendEndOfRow(CopyState cstate)
466466
#endif
467467
}
468468

469-
(void) fwrite(fe_msgbuf->data, fe_msgbuf->len,
470-
1, cstate->copy_file);
471-
if (ferror(cstate->copy_file))
469+
if (fwrite(fe_msgbuf->data, fe_msgbuf->len, 1,
470+
cstate->copy_file) != 1 ||
471+
ferror(cstate->copy_file))
472472
ereport(ERROR,
473473
(errcode_for_file_access(),
474474
errmsg("could not write to COPY file: %m")));

src/backend/postmaster/pgstat.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3284,6 +3284,7 @@ pgstat_write_statsfile(bool permanent)
32843284
int32 format_id;
32853285
const char *tmpfile = permanent ? PGSTAT_STAT_PERMANENT_TMPFILE : pgstat_stat_tmpname;
32863286
const char *statfile = permanent ? PGSTAT_STAT_PERMANENT_FILENAME : pgstat_stat_filename;
3287+
int rc;
32873288

32883289
/*
32893290
* Open the statistics temp file to write out the current values.
@@ -3307,12 +3308,14 @@ pgstat_write_statsfile(bool permanent)
33073308
* Write the file header --- currently just a format ID.
33083309
*/
33093310
format_id = PGSTAT_FILE_FORMAT_ID;
3310-
fwrite(&format_id, sizeof(format_id), 1, fpout);
3311+
rc = fwrite(&format_id, sizeof(format_id), 1, fpout);
3312+
(void) rc; /* we'll check for error with ferror */
33113313

33123314
/*
33133315
* Write global stats struct
33143316
*/
3315-
fwrite(&globalStats, sizeof(globalStats), 1, fpout);
3317+
rc = fwrite(&globalStats, sizeof(globalStats), 1, fpout);
3318+
(void) rc; /* we'll check for error with ferror */
33163319

33173320
/*
33183321
* Walk through the database table.
@@ -3326,7 +3329,8 @@ pgstat_write_statsfile(bool permanent)
33263329
* use to any other process.
33273330
*/
33283331
fputc('D', fpout);
3329-
fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
3332+
rc = fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
3333+
(void) rc; /* we'll check for error with ferror */
33303334

33313335
/*
33323336
* Walk through the database's access stats per table.
@@ -3335,7 +3339,8 @@ pgstat_write_statsfile(bool permanent)
33353339
while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
33363340
{
33373341
fputc('T', fpout);
3338-
fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
3342+
rc = fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
3343+
(void) rc; /* we'll check for error with ferror */
33393344
}
33403345

33413346
/*
@@ -3345,7 +3350,8 @@ pgstat_write_statsfile(bool permanent)
33453350
while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&fstat)) != NULL)
33463351
{
33473352
fputc('F', fpout);
3348-
fwrite(funcentry, sizeof(PgStat_StatFuncEntry), 1, fpout);
3353+
rc = fwrite(funcentry, sizeof(PgStat_StatFuncEntry), 1, fpout);
3354+
(void) rc; /* we'll check for error with ferror */
33493355
}
33503356

33513357
/*

src/backend/utils/error/elog.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,10 +1747,12 @@ write_eventlog(int level, const char *line, int len)
17471747
static void
17481748
write_console(const char *line, int len)
17491749
{
1750+
int rc;
1751+
17501752
#ifdef WIN32
17511753

17521754
/*
1753-
* WriteConsoleW() will fail of stdout is redirected, so just fall through
1755+
* WriteConsoleW() will fail if stdout is redirected, so just fall through
17541756
* to writing unconverted to the logfile in this case.
17551757
*
17561758
* Since we palloc the structure required for conversion, also fall
@@ -1788,13 +1790,18 @@ write_console(const char *line, int len)
17881790
#else
17891791

17901792
/*
1791-
* Conversion on non-win32 platform is not implemented yet. It requires
1793+
* Conversion on non-win32 platforms is not implemented yet. It requires
17921794
* non-throw version of pg_do_encoding_conversion(), that converts
17931795
* unconvertable characters to '?' without errors.
17941796
*/
17951797
#endif
17961798

1797-
write(fileno(stderr), line, len);
1799+
/*
1800+
* We ignore any error from write() here. We have no useful way to report
1801+
* it ... certainly whining on stderr isn't likely to be productive.
1802+
*/
1803+
rc = write(fileno(stderr), line, len);
1804+
(void) rc;
17981805
}
17991806

18001807
/*
@@ -2457,13 +2464,30 @@ send_message_to_server_log(ErrorData *edata)
24572464

24582465
/*
24592466
* Send data to the syslogger using the chunked protocol
2467+
*
2468+
* Note: when there are multiple backends writing into the syslogger pipe,
2469+
* it's critical that each write go into the pipe indivisibly, and not
2470+
* get interleaved with data from other processes. Fortunately, the POSIX
2471+
* spec requires that writes to pipes be atomic so long as they are not
2472+
* more than PIPE_BUF bytes long. So we divide long messages into chunks
2473+
* that are no more than that length, and send one chunk per write() call.
2474+
* The collector process knows how to reassemble the chunks.
2475+
*
2476+
* Because of the atomic write requirement, there are only two possible
2477+
* results from write() here: -1 for failure, or the requested number of
2478+
* bytes. There is not really anything we can do about a failure; retry would
2479+
* probably be an infinite loop, and we can't even report the error usefully.
2480+
* (There is noplace else we could send it!) So we might as well just ignore
2481+
* the result from write(). However, on some platforms you get a compiler
2482+
* warning from ignoring write()'s result, so do a little dance with casting
2483+
* rc to void to shut up the compiler.
24602484
*/
24612485
static void
24622486
write_pipe_chunks(char *data, int len, int dest)
24632487
{
24642488
PipeProtoChunk p;
2465-
24662489
int fd = fileno(stderr);
2490+
int rc;
24672491

24682492
Assert(len > 0);
24692493

@@ -2476,7 +2500,8 @@ write_pipe_chunks(char *data, int len, int dest)
24762500
p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'F' : 'f');
24772501
p.proto.len = PIPE_MAX_PAYLOAD;
24782502
memcpy(p.proto.data, data, PIPE_MAX_PAYLOAD);
2479-
write(fd, &p, PIPE_HEADER_SIZE + PIPE_MAX_PAYLOAD);
2503+
rc = write(fd, &p, PIPE_HEADER_SIZE + PIPE_MAX_PAYLOAD);
2504+
(void) rc;
24802505
data += PIPE_MAX_PAYLOAD;
24812506
len -= PIPE_MAX_PAYLOAD;
24822507
}
@@ -2485,7 +2510,8 @@ write_pipe_chunks(char *data, int len, int dest)
24852510
p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'T' : 't');
24862511
p.proto.len = len;
24872512
memcpy(p.proto.data, data, len);
2488-
write(fd, &p, PIPE_HEADER_SIZE + len);
2513+
rc = write(fd, &p, PIPE_HEADER_SIZE + len);
2514+
(void) rc;
24892515
}
24902516

24912517

src/bin/psql/common.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ static void
228228
handle_sigint(SIGNAL_ARGS)
229229
{
230230
int save_errno = errno;
231+
int rc;
231232
char errbuf[256];
232233

233234
/* if we are waiting for input, longjmp out of it */
@@ -244,11 +245,16 @@ handle_sigint(SIGNAL_ARGS)
244245
if (cancelConn != NULL)
245246
{
246247
if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
247-
write_stderr("Cancel request sent\n");
248+
{
249+
rc = write_stderr("Cancel request sent\n");
250+
(void) rc; /* ignore errors, nothing we can do here */
251+
}
248252
else
249253
{
250-
write_stderr("Could not send cancel request: ");
251-
write_stderr(errbuf);
254+
rc = write_stderr("Could not send cancel request: ");
255+
(void) rc; /* ignore errors, nothing we can do here */
256+
rc = write_stderr(errbuf);
257+
(void) rc; /* ignore errors, nothing we can do here */
252258
}
253259
}
254260

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy