472,993 Members | 3,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

preventing following stdout getting into the stdin-stream

Hello,

Some time ago I tried to find a solution for preventing buffer overflows
in stdin. I thought getc was the solution but today I came to a problem.
I wanted to use my cognitions for a prompt but I recognized that the
following stdout of my program is used as stdin for the stream. So I
couldn't enter text, because the text was already read in (due to this
following stdout).
I thought about stopping the program-flow before stdin reads the whole
following stdout, so I set just a scanf before, and it works without
problems.

int c, i = 0;
char publisher[21];

printf("Enter the publisher (max. 20): ");
scanf("%c", &c);
while ( ( (c = getchar()) != '\n') && (i < 20) ) {
publisher[i++] = c;
publisher[i] = '\0';
}

Although my solution seems to work perfectly, I'm still a little bit
anxious about scanf, because I'm not exactly sure what's happening
between scanf and the line after.
Actually every entry by the user is a buffer overflow, but it's fielded
due to the next line. Did I understand that right? Could there appear
any non-predictable errors or is this safe?

Thanks,
Markus
Feb 19 '06 #1
2 1583

Markus Pitha wrote:
Hello,

Some time ago I tried to find a solution for preventing buffer overflows
in stdin. I thought getc was the solution but today I came to a problem.
I wanted to use my cognitions for a prompt but I recognized that the
following stdout of my program is used as stdin for the stream. So I
couldn't enter text, because the text was already read in (due to this
following stdout).
I thought about stopping the program-flow before stdin reads the whole
following stdout, so I set just a scanf before, and it works without
problems. May not be what you expected.
int c, i = 0;
char publisher[21];

printf("Enter the publisher (max. 20): ");
scanf("%c", &c);
When I compiled it with GCC, there was an warning:
warning: format '%c' expects type 'char *', but argument 2 has
type 'int *'.
while ( ( (c = getchar()) != '\n') && (i < 20) ) {
publisher[i++] = c;
publisher[i] = '\0';
}

Although my solution seems to work perfectly, I'm still a little bit
anxious about scanf, because I'm not exactly sure what's happening
between scanf and the line after.
I think it doesn't seem to work. You may miss the first character
because
"scanf()" eats it.
IMHO, it may work to replace "scanf()" with "fflush(stdout)".
Actually every entry by the user is a buffer overflow, but it's fielded
due to the next line. Did I understand that right? Could there appear
any non-predictable errors or is this safe?

Thanks,
Markus


Feb 20 '06 #2
Hello,

kernelxu schrieb:
May not be what you expected.
Obviously, you are right.
I think it doesn't seem to work. You may miss the first character
because
"scanf()" eats it.
IMHO, it may work to replace "scanf()" with "fflush(stdout)".


Yes, that's the problem. I didn't recongnize it first, but I'll try
fflush. Maybe you are right.
Markus.
Feb 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
4
by: Jan Knop | last post by:
Hello I am writing a Windows application where I need to redirect stdin, stdout and stderr from Python. to my application Is it a simple way of do it ? Has anyone done it using Winsock ?
5
by: Jean-Pierre Bergamin | last post by:
Dear python-Community We are forced to use a quite old simulation software that's based on Modula-2. The idea is now to let this software "talk" to the outside world over a TCP/IP network. ...
6
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* ....
0
by: lickspittle | last post by:
Hi, I have Python embedded with my other code, and when my other code opens a console and redirects stdout, stdin and stderr to it, then calls PyRun_InteractiveLoop, it immediately returns with...
3
by: Harayasu | last post by:
Hi, Using fgets() I can read from stdin and with fputs() I can write to stdout. Now I have two programs, one writing to stdin and the other one reading from stdin. And I would like the second...
31
by: Randy Yates | last post by:
When I try fprintf(file, "Report of File %s\n", sMF->fileName); I get a core dump. This works fine if fprintf is changed to sprintf and a string buffer used instead as the first parameter. ...
1
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
13
by: Vincent Delporte | last post by:
Hi I'm a Python newbie, and would like to rewrite this Perl scrip to be run with the Asterisk PBX: http://www.voip-info.org/wiki/view/Asterisk+NetCID Anyone knows if those lines are...
5
by: dave_140390 | last post by:
Hi, I have problems getting my Python code to work with UTF-8 encoding when reading from stdin / writing to stdout. Say I have a file, utf8_input, that contains a single character, , coded...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.