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 2 1600
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 ?
|
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.
...
|
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.* ....
|
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...
|
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...
|
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.
...
|
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
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |