mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "wangjianjian (C)" <wangjianjian3@huawei.com>
To: "musl@lists.openwall.com" <musl@lists.openwall.com>
Cc: "Songyunlong (Euler)" <yunlong.song@huawei.com>
Subject: [PATCH] ftello: Need adjust file offset before switching to write
Date: Wed, 18 Dec 2019 10:18:16 +0000	[thread overview]
Message-ID: <07082d7e15024b06ba261fbb4e29eac5@huawei.com> (raw)

Consider below code flow:

FILE *fp = fopen("foobar", "w+");
fputs("hello", fp);
rewind(fp);
fgetc(fp);
ungetc('x', fp);
fputs("world", fp);
off = ftello(fp);

In this code snippet, 'off' will be 10 at last which should be 5 and
file 'foobar's content will be 'helloworld' which is not correct.

The reason is that when do read, we will try our best to get more data
to fill user-provided and the default stream buffer so that the
underlying file offset may not be consistent with stream offset.
As an example above, 'fgetc' will read the whole 5 bytes from 'foobar'
even though we just would like to read one char.

And when we switch to write, we need to adjust the file offset by
decreasing the size of already buffered but not readed data.

Signed-off-by: Wang Jianjian <wangjianjian3@huawei.com>
---
 src/stdio/__towrite.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/stdio/__towrite.c b/src/stdio/__towrite.c
index 4c9c66ae..dde3c967 100644
--- a/src/stdio/__towrite.c
+++ b/src/stdio/__towrite.c
@@ -7,6 +7,12 @@ int __towrite(FILE *f)
 		f->flags |= F_ERR;
 		return EOF;
 	}
+
+	/* Adjust offset for already buffered but not readed data */
+	if (f->rend - f->rpos > 0 &&
+	    f->seek(f, f->rpos - f->rend, SEEK_CUR) < 0)
+		return EOF;
+
 	/* Clear read buffer (easier than summoning nasal demons) */
 	f->rpos = f->rend = 0;


BR,
Wang Jianjian



             reply	other threads:[~2019-12-18 10:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-18 10:18 wangjianjian (C) [this message]
2019-12-18 13:46 ` Rich Felker
2019-12-19  2:47   ` 答复: [musl] " wangjianjian (C)
2019-12-19  3:03     ` Rich Felker
2019-12-19  3:19       ` 答复: " wangjianjian (C)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=07082d7e15024b06ba261fbb4e29eac5@huawei.com \
    --to=wangjianjian3@huawei.com \
    --cc=musl@lists.openwall.com \
    --cc=yunlong.song@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).