From: Ralph Corderoy Date: Wed, 9 Aug 2017 12:50:31 +0000 (+0100) Subject: m_getfld.c: Check ftello(3) and fseeko(3) for errors. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/03e153a00cefc6ad1f2f73c6f3c78aec6d67a602?ds=sidebyside;hp=e33979708ffdf9aa7beafcd67479374120530cfb m_getfld.c: Check ftello(3) and fseeko(3) for errors. They were being ignored. Exit on failure; harsh, but we don't know the circumstances where they might occur and thus what recovery would be apt. --- diff --git a/sbr/m_getfld.c b/sbr/m_getfld.c index 18e38d04..9ea6f070 100644 --- a/sbr/m_getfld.c +++ b/sbr/m_getfld.c @@ -409,7 +409,8 @@ enter_getfld (m_getfld_state_t *gstate, FILE *iob) { if (!s->track_filepos) return; - pos = ftello(iob); + if ((pos = ftello(iob)) == -1) + adios("getfld's iob", "failed to get offset on entry"); if (pos == 0 && s->last_internal_pos == 0) return; @@ -438,11 +439,15 @@ enter_getfld (m_getfld_state_t *gstate, FILE *iob) { s->total_bytes_read += pos_movement; pos = s->last_internal_pos; } else { + off_t off; size_t num_read; /* This seek skips past an integral number of chunks of size MSG_INPUT_SIZE. */ - fseeko (iob, pos/MSG_INPUT_SIZE * MSG_INPUT_SIZE, SEEK_SET); + off = pos / MSG_INPUT_SIZE * MSG_INPUT_SIZE; + if (fseeko(iob, off, SEEK_SET) == -1) + adios("getfld's iob", "failed to set offset to skip: %d", + off); num_read = fread (s->msg_buf, 1, MSG_INPUT_SIZE, iob); s->readpos = s->msg_buf + pos % MSG_INPUT_SIZE; s->end = s->msg_buf + num_read; @@ -450,7 +455,8 @@ enter_getfld (m_getfld_state_t *gstate, FILE *iob) { } } - fseeko (iob, pos, SEEK_SET); + if (fseeko(iob, pos, SEEK_SET) == -1) + adios("getfld's iob", "failed to set offset on entry: %d", pos); } static void @@ -459,11 +465,15 @@ leave_getfld (m_getfld_state_t s) { if (s->track_filepos) { /* Save the internal file position that we use for the input buffer. */ - s->last_internal_pos = ftello (s->iob); + if ((s->last_internal_pos = ftello(s->iob)) == -1) + adios("getfld's iob", "failed to get offset before seek"); /* Set file stream position so that callers can use ftell(). */ - fseeko (s->iob, s->total_bytes_read, SEEK_SET); - s->last_caller_pos = ftello (s->iob); + if (fseeko(s->iob, s->total_bytes_read, SEEK_SET) == -1) + adios("getfld's iob", "failed to set offset: %d", + s->total_bytes_read); + if ((s->last_caller_pos = ftello(s->iob)) == -1) + adios("getfld's iob", "failed to get offset after seek"); } }