473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

safety and C

I'm reading a book on safety.
Do you know that?
printf("Use:> %s prog\n", argv[0] );
or
printf("Use:> %s prog\n", str_in_input);
is danger.
If str_in_input=" %x " this would print the return address of printf.
If str_in_input=" %x%x%x%x%x%x%x%x%x%x%x%x%x " it could see the stack.
and what If in the stack there is a pointer to a string password?
Nov 14 '05 #1
7 1362
RoSsIaCrIiLoIA wrote:
I'm reading a book on safety.
Do you know that?
printf("Use:> %s prog\n", argv[0] );
or
printf("Use:> %s prog\n", str_in_input);
is danger.
If str_in_input=" %x " this would print the return address of printf.
It will print "Use:> %x prog\n"
If str_in_input=" %x%x%x%x%x%x%x%x%x%x%x%x%x " it could see the stack.
It will print "Use:> %x%x%x%x%x%x%x%x%x%x%x%x%x prog\n"
and what If in the stack there is a pointer to a string password?


Nope it cannot, above is perfectly safe.

-- Thomas.

Nov 14 '05 #2
RoSsIaCrIiLoIA wrote:
I'm reading a book on safety.
Do you know that?
printf("Use:> %s prog\n", argv[0] );
or
printf("Use:> %s prog\n", str_in_input);
is danger.
If str_in_input=" %x " this would print the return address of printf.
If str_in_input=" %x%x%x%x%x%x%x%x%x%x%x%x%x " it could see the stack.
and what If in the stack there is a pointer to a string password?


Where are you getting this crap? See the program below and its output:

#include <stdio.h>
int main(void)
{
char *str_in_input;
printf("[output]\n");
str_in_input = " %x ";
printf("Use:> %s prog\n", str_in_input);
str_in_input = " %x%x%x%x%x%x%x%x%x%x%x%x%x ";
printf("Use:> %s prog\n", str_in_input);
return 0;
}

[output]
Use:> %x prog
Use:> %x%x%x%x%x%x%x%x%x%x%x%x%x prog
Nov 14 '05 #3
On Wed, 14 Apr 2004 08:57:21 +0100, Thomas stegen
<ts*****@cis.strath.ac.uk> wrote:
RoSsIaCrIiLoIA wrote:
I'm reading a book on safety.
Do you know that?
printf("Use:> %s prog\n", argv[0] );
or
printf("Use:> %s prog\n", str_in_input);
is danger.
If str_in_input=" %x " this would print the return address of printf.


It will print "Use:> %x prog\n"


Yes but this print
Inserisci una stringa> %x %hx %hx %hx %hx %hx %hx %hx %hx
40b034 f000 7825 7868 7868 7868 7868 7868 7868
Is it possible to exit from s?

#include <stdio.h>

int main(void)
{char s[50] = {0};
int d = 0;
while( 1 )
{
printf("Inserisci una stringa> "); fflush(stdout);
fgets(s, sizeof s, stdin);
printf(s);
if(*s=='1') break;
}
return 0;
}

Nov 14 '05 #4
RoSsIaCrIiLoIA wrote:
On Wed, 14 Apr 2004 08:57:21 +0100, Thomas stegen
It will print "Use:> %x prog\n"

Yes but this print


Of course it does.
Inserisci una stringa> %x %hx %hx %hx %hx %hx %hx %hx %hx
40b034 f000 7825 7868 7868 7868 7868 7868 7868
Is it possible to exit from s?
s is a string, there is no entrance or exit...
printf("Inserisci una stringa> "); fflush(stdout);
fgets(s, sizeof s, stdin);
printf(s);


I trust you see the difference between the above
and printf("%s", s) ?

Or just use puts.

--
Thomas.
Nov 14 '05 #5
RoSsIaCrIiLoIA a écrit :
On Wed, 14 Apr 2004 08:57:21 +0100, Thomas stegen
<ts*****@cis.strath.ac.uk> wrote:

RoSsIaCrIiLoIA wrote:

I'm reading a book on safety.
Do you know that?
printf("Use:> %s prog\n", argv[0] );
or
printf("Use:> %s prog\n", str_in_input);
is danger.
If str_in_input=" %x " this would print the return address of printf.


It will print "Use:> %x prog\n"

Yes but this print
Inserisci una stringa> %x %hx %hx %hx %hx %hx %hx %hx %hx
40b034 f000 7825 7868 7868 7868 7868 7868 7868
Is it possible to exit from s?

#include <stdio.h>

int main(void)
{char s[50] = {0};
int d = 0;
while( 1 )
{
printf("Inserisci una stringa> "); fflush(stdout);
fgets(s, sizeof s, stdin);
printf(s);
if(*s=='1') break;
}
return 0;
}


Indeed printf(s); may be unsafe if s can be entered by a malevolent
person. However printf("%s", s); or the similar versions in your first
message are not.

--
Richard
Nov 14 '05 #6
In <c5************@ID-228872.news.uni-berlin.de> Thomas stegen <ts*****@cis.strath.ac.uk> writes:
RoSsIaCrIiLoIA wrote:
On Wed, 14 Apr 2004 08:57:21 +0100, Thomas stegen
It will print "Use:> %x prog\n"

Yes but this print


Of course it does.
Inserisci una stringa> %x %hx %hx %hx %hx %hx %hx %hx %hx
40b034 f000 7825 7868 7868 7868 7868 7868 7868
Is it possible to exit from s?


s is a string, there is no entrance or exit...
printf("Inserisci una stringa> "); fflush(stdout);
fgets(s, sizeof s, stdin);
printf(s);


I trust you see the difference between the above
and printf("%s", s) ?


If he did, he wouldn't have posted the original bullshit in the first
place...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
RoSsIaCrIiLoIA <n@esiste.ee> writes:
I'm reading a book on safety.
Do you know that?
printf("Use:> %s prog\n", argv[0] );
or
printf("Use:> %s prog\n", str_in_input);
is danger.
If str_in_input=" %x " this would print the return address of printf.
If str_in_input=" %x%x%x%x%x%x%x%x%x%x%x%x%x " it could see the stack.
and what If in the stack there is a pointer to a string password?


Either your book is incorrect, or you've misunderstood it. There is
an issue here, but your examples don't demonstrate it.

Here's a simple program that prints its first command-line argument,
if any, followed by a newline:

#include <stdio.h>
int main(int argc, char **argv)
{
if (argc >= 2) {
printf("%s", argv[1]);
}
else {
printf("(no arguments)");
}
printf("\n");
return 0;
}

This is perfectly safe (at least for our current purposes).

Here's another version of the same program:

#include <stdio.h>
int main(int argc, char **argv)
{
if (argc >= 2) {
printf(argv[1]); /* DANGER!! */
}
else {
printf("(no arguments)");
}
printf("\n");
return 0;
}

On the line marked "DANGER!!", it uses an unchecked string as the
format argument to printf(). If I run this program with an argument
like "foobar" or "42", it will work exactly the same way as the first
version. If the argument happens to contain a printf format, though,
like "%s", it invokes undefined, because it tells printf() to look for
a second argument that wasn't actually passed to it.

The canonical first program contains the line
printf("hello, world\n");
It could instead be written as
printf("%s\n", "hello, world");
or
printf("%s", "hello, world\n");
but it really doesn't make any difference; since the format string is
a literal, we can tell by inspection that it doesn't contain any
conversion specifiers. If the string comes from an outside source,
such as the command line or standard input, we can't make that
assumption, so we need to use "%s" to guarantee that any specifiers
that happen to be in the string are just printed, not interpreted.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #8

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

Similar topics

5
by: John Harrison | last post by:
Where can I find information on exception safety in the STL? I.e. which methods on which types offer what level of exception safety. Josuttis has a useful list of classes and methods but he fails...
2
by: Steve Jorgensen | last post by:
I frequently find myself wanting to use class abstraction in VB/VBA code, and frankly, with the tacked-on class support in VB/VBA, there are some problems with trying to do that and have any...
2
by: Dave | last post by:
Hello all, I am creating a linked list implementation which will be used in a number of contexts. As a result, I am defining its value node as type (void *). I hope to pass something in to its...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
9
by: Alexander Fleck | last post by:
Hi, I' ve to make a software module thread safe. I know how to realize that and what' re the main topics of thread safety. But I don' t know how thread safety can be tested. I read about a test...
2
by: Howard Swope | last post by:
Could someone help explain thread safety issues in the System.Collections classes? The documentation states:...
4
by: The Crow | last post by:
for example i have static readonly SqlParameter and i want to clone them at runtime. as clone operation will not write to SqlParameter object, just reading, should i lock that object during read...
18
by: aarklon | last post by:
In the article http://en.wikipedia.org/wiki/Type_safety it is written as The archetypal type-unsafe language is C because (for example) it is possible for an integer to be viewed as a function...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
0
by: Graham Wideman | last post by:
Folks: Can anyone tell me what controls php's "thread safety" feature? I have an installation where phpinfo() is showing Thread safety: enabled, whereas I need it disabled in order to work...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.