From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9274 Path: news.gmane.org!not-for-mail From: hombre Newsgroups: gmane.linux.lib.musl.general Subject: bug in fwrite/__towrite Date: Fri, 5 Feb 2016 16:32:58 +0100 Message-ID: <56B4C0AA.3020705@gmx.at> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1454686392 11874 80.91.229.3 (5 Feb 2016 15:33:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 5 Feb 2016 15:33:12 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-9287-gllmg-musl=m.gmane.org@lists.openwall.com Fri Feb 05 16:33:12 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1aRiNk-0000uo-8R for gllmg-musl@m.gmane.org; Fri, 05 Feb 2016 16:33:12 +0100 Original-Received: (qmail 16321 invoked by uid 550); 5 Feb 2016 15:33:08 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 16277 invoked from network); 5 Feb 2016 15:33:04 -0000 User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 X-Provags-ID: V03:K0:uDRkVUWcIuOEe7KlTNGa+eyu1lSwatOPZbSthJWoiEj8s+uZkTN gDMXkxEzSQaQb31njDpT4kGwiaAuuz1KBmcmlQLKZ06KiN1TgOxkkReJJOjtW+0MwzFPvpE ldPM/s9cuqKi6rMG8SreQ2/e7w7eTz2v7wI/UhzNkfOiQVL70w6ApRDY77SB1H9uhsmSKum kKGH0AXZBmmU06ux14mOw== X-UI-Out-Filterresults: notjunk:1;V01:K0:+qHZOcdo6Do=:92w3TQVHJVkT4IFrs8gxac pC7Q49ANiqSqseDL2RKYTbA6XWBipYp1adfndC0Fg3vhwuKt0hS6XLPqNpcO5hRVeOjFKooTm bx8VAJqG05ECVRWFdS4PJJ7CX2TqSgkBaWN2jSeBhCs32M/2YddBnzuBtPuN23Msgf5za4gHa hRRHYEU1st0oOCk90AG+GDww+/7jS/U+mMikDdgrwLBI1jhaYFobAfUXjP8iHMyT+9/iOYBFT qRjiEKh/mVyN23sJ+9qfKhoUyg7NA5tt5QfZKgyKuLhRfc3FW1BCRmUyH8WDmO2Mc9XQV9utS 736PFNC4NYlXBPIHsjbu2Oi/EqvCJabtORwZVt6ydHgo8+mBln1wTY2AE5q2XqiZvt9uHD8Nn stFX+6QPKkiYqeYU7rHJ6wgKugJ4b3Nr6Gjiu0D6/BZVnjMaCpLhsIWn6BmOFD7rMNwo7SOby z2wHZ+QrhF930ey5oZW0maVplzMX8aP3I9esBfHWh6ch2E5rrVqwiDyMjYQHF7iFc+XdlzXUa 1OD1c3xX5oe+JP1VcWElAG24oK5JSrsltfINgLUyibTmL78yEqG0255pDa9GYIRpIiyR7lSL2 Qn4SdJnPgKe6339jgPuZW0tgpe7CxRDmpXBQhVEAj+2IyRWXlahIT2+WqDN8HMeManm+HvLZ3 XHbU5ideI+F2tsaR/abiZujf+Ut6eIeFO6XHDN5y6ASTK/ijGNazC9KD7dXpPMKeqNPZtOoU4 cZQuWPjGwL27HkRbd91c6N5cXDCXZVWU3+N9CY40qJbFXkiUUwoEJ7arfLqE6gUnqooxcbCM Xref: news.gmane.org gmane.linux.lib.musl.general:9274 Archived-At: Hello, I think there is a bug in fwrite/__towrite. This is my unittest that fails: static void test_write_read2(const char *fname) { char wbuf[3]; char c; FILE *file = fopen(fname, "wb"); assert(file != NULL); wbuf[0] = 'a'; wbuf[1] = 'b'; wbuf[2] = 'c'; size_t written = fwrite(wbuf, 1, 3, file); assert(written == 3); fclose(file); file = fopen(fname, "rb+"); size_t nread = fread(&c, 1, 1, file); assert(nread == 1); assert(c == 'a'); c = 'B'; written = fwrite(&c, 1, 1, file); assert(written == 1); nread = fread(&c, 1, 1, file); assert(nread == 1); /* <================== nread is 0 here ! */ assert(c == 'c'); fclose(file); } Please note that I have not tested this with the original musl-libc in linux. I found this bug while I was trying to port parts of musl to a small embedded os. But I think the bug is not in my port. Here is what I think is wrong: - fwrite calls __towrite when the write buffer is not active if (!f->wend && __towrite(f)) return 0; - __towrite clears the read buffer, but the underlying filepointer is not adjusted. I think that the filepointer should be adjusted, when the read buffer is not empty. int __towrite(FILE *f) { ... /* Clear read buffer (easier than summoning nasal demons) */ f->rpos = f->rend = 0; ... Here is my fix: int __towrite(FILE *f) { ... /* Clear read buffer (easier than summoning nasal demons) */ if (f->rpos) { /* Adjust underlying filepointer for unread data in buffer. */ if (f->seek(f, -(f->rend - f->rpos), SEEK_CUR) < 0) return -1; f->rpos = f->rend = 0; } ... Regards, Erwin