DaemonForums  

Go Back   DaemonForums > OpenBSD > OpenBSD General

OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 13th August 2020
phillbush's Avatar
phillbush phillbush is offline
Port Guard
 
Join Date: Apr 2020
Posts: 15
Default Memory stream behavior in OpenBSD different from what is shown in APUE

I am reading APUE (Advanced Programming in the UNIX Environment) and I am testing a sample program whose output produced on OpenBSD differs from the output shown in the book.

Here is the program:

Code:
#include "apue.h"

#define BSZ 48

int
main(void)
{
	FILE *fp;
	char buf[BSZ];

	memset(buf, 'a', BSZ-2);
	buf[BSZ-2] = '\0';
	buf[BSZ-1] = 'X';
	if ((fp = fmemopen(buf, BSZ, "w+")) == NULL)
		err_sys("fmemopen failed");
	printf("initial buffer contents: %s\n", buf);
	fprintf(fp, "hello, world");
	printf("before flush: %s\n", buf);
	fflush(fp);
	printf("after fflush: %s\n", buf);
	printf("len of string in buf = %ld\n", (long)strlen(buf));

	memset(buf, 'b', BSZ-2);
	buf[BSZ-2] = '\0';
	buf[BSZ-1] = 'X';
	fprintf(fp, "hello, world");
	fseek(fp, 0, SEEK_SET);
	printf("after  fseek: %s\n", buf);
	printf("len of string in buf = %ld\n", (long)strlen(buf));

	memset(buf, 'c', BSZ-2);
	buf[BSZ-2] = '\0';
	buf[BSZ-1] = 'X';
	fprintf(fp, "hello, world");
	fclose(fp);
	printf("after fclose: %s\n", buf);
	printf("len of string in buf = %ld\n", (long)strlen(buf));

	return(0);
}

This is what it prints in OpenBSD:

Code:
initial buffer contents: 
before flush: 
after fflush: hello, worldaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
len of string in buf = 46
after  fseek: bbbbbbbbbbbbhello, worldbbbbbbbbbbbbbbbbbbbbbb
len of string in buf = 46
after fclose: hello, worldcccccccccccccccccccccccccccccccccc
len of string in buf = 46
This is what the book shows:

Code:
initial buffer contents: 
before flush: 
after fflush: hello, world
len of string in buf = 12
after  fseek: bbbbbbbbbbbbhello, world
len of string in buf = 24
after fclose: hello, worldcccccccccccccccccccccccccccccccccc
len of string in buf = 46
Is there something different in OpenBSD implementation of memory streams?
It was supposed to write a `'\0'` (nul character) on the two first writings after `"hello, world"`, but this nul character is not written. And only the nul character I explicitly wrote with the statement `buf[BSZ-2] = '\0';` is considered by the printf(3)s.
But the nul character that should be written at opening the memory stream with fmemopen(3) is correctly written at the beginning of the buffer, as you can see in the line `before flush:`.
Reply With Quote
  #2   (View Single Post)  
Old 14th August 2020
bsdun bsdun is offline
Real Name: Steve
Fdisk Soldier
 
Join Date: Feb 2020
Posts: 48
Default

http://www.apuebook.com/
I suggest visiting their official website and looking for Errata for your edition of the book. Download the source code from the book and test the same file.
Reply With Quote
  #3   (View Single Post)  
Old 14th August 2020
ibara ibara is offline
OpenBSD language porter
 
Join Date: Jan 2014
Posts: 783
Default

phillbush did the right thing, filed a good bug report, and looks like things are going to be fixed: https://marc.info/?l=openbsd-bugs&m=159736580430716&w=2

phillbush then filed another good bug report about a potential bug in vis(1), though as of now it has not yet been triaged: https://marc.info/?l=openbsd-bugs&m=159742819812003&w=2

This is how OpenBSD gets better. I'd like to encourage everyone to follow this lead if you find things you're not sure about.
Reply With Quote
  #4   (View Single Post)  
Old 14th August 2020
phillbush's Avatar
phillbush phillbush is offline
Port Guard
 
Join Date: Apr 2020
Posts: 15
Default

Quote:
Originally Posted by bsdun View Post
http://www.apuebook.com/
I suggest visiting their official website and looking for Errata for your edition of the book. Download the source code from the book and test the same file.
Quote:
Originally Posted by ibara View Post
phillbush did the right thing, filed a good bug report, and looks like things are going to be fixed: https://marc.info/?l=openbsd-bugs&m=159736580430716&w=2
It was not a bug on APUE (also there is no citation to it in the errata), but on OpenBSD's implementation of fmemopen(3). It has been already fixed here. The problem was that the stream was not truncated if opened with "w+" mode, only if opened with "w" mode. I posted it here (and on r/openbsd) before posting on the mailing list in order to have certainty it is indeed a bug.

Quote:
Originally Posted by ibara View Post
phillbush then filed another good bug report about a potential bug in vis(1), though as of now it has not yet been triaged: https://marc.info/?l=openbsd-bugs&m=159742819812003&w=2
This bug occurs when the input of vis contains a tab and it is asked to fold at a short foldwidth (equal to or less than 8), then it goes through a infinite loop. I identified the cause of the problem to be a goto in the source file.

Quote:
Originally Posted by ibara View Post
This is how OpenBSD gets better. I'd like to encourage everyone to follow this lead if you find things you're not sure about.
Thanks . Unfortunately I have not familiarity with cvs in order to produce a decent diff, nor I have set mail(1) in order to use sendbug(1).
Reply With Quote
  #5   (View Single Post)  
Old 15th August 2020
ibara ibara is offline
OpenBSD language porter
 
Join Date: Jan 2014
Posts: 783
Default

Quote:
Originally Posted by phillbush View Post
Thanks . Unfortunately I have not familiarity with cvs in order to produce a decent diff, nor I have set mail(1) in order to use sendbug(1).
You can make git diffs against the OpenBSD GitHub mirror: https://github.com/openbsd/src
Reply With Quote
Reply

Tags
apue, bug, fmemopen, openbsd

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python 3 strange behavior victorvas Programming 1 22nd December 2019 11:46 AM
apropos(1) behavior beavers OpenBSD General 10 23rd September 2019 07:35 AM
OpenBSD on HP Stream 14 cb112wm shep OpenBSD General 5 1st December 2018 04:04 PM
SSH is being blocked from WAN however public IP shown in server log sparky OpenBSD Security 3 29th October 2012 01:29 PM
strange behavior of PF majkelos OpenBSD Security 2 23rd October 2011 06:23 PM


All times are GMT. The time now is 01:44 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick