LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-12-2012, 09:04 AM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,815

Rep: Reputation: 503Reputation: 503Reputation: 503Reputation: 503Reputation: 503Reputation: 503

1. right here, in this trivial example sprintf isn't needed either, it could have been:
Code:
system (EDITOR " myfile.txt");
but in actual applications file-name comes from the user and its length is not limited, so any fix-sized buffer can be overwritten by malicious user.

2. I won't argue about goto here, there are separate topics for that.
 
Old 09-12-2012, 09:04 AM   #17
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 314

Rep: Reputation: 77
Code:
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#define EDITOR "nano"

int systemf (const char *fmt, ...)
{
    char *cmdbuff= NULL;
    va_list ap;
    int rc;

    va_start (ap, fmt);
    rc= vasprintf (&cmdbuff, fmt, ap);
    va_end (ap);

    if (rc<0)
        fprintf (stderr, "systemf: *** vasprintf error\n");
    else
        rc= system (cmdbuff);

    if (cmdbuff) free (cmdbuff);
    return rc;
}

int main (void)
{
static const char filename[] = "myfile.txt";

    systemf ("%s %s", EDITOR, filename);
    return 0;
}

Look how many nested if I had to add! That's just lol
 
Old 09-12-2012, 09:06 AM   #18
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 314

Rep: Reputation: 77
Quote:
Originally Posted by NevemTeve View Post
1. right here, in this trivial example sprintf isn't needed either, it could have been:
Code:
system (EDITOR " myfile.txt");
but in actual applications file-name comes from the user and its length is not limited, so any fix-sized buffer can be overwritten by malicious user.
That's not true, the filename array is 128 long. So buffer overflow is impossible (if you use fgets to get it)
 
Old 09-12-2012, 12:20 PM   #19
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,815

Rep: Reputation: 503Reputation: 503Reputation: 503Reputation: 503Reputation: 503Reputation: 503
It's 9999 in the original post, but it has no importance, as it is not a real usage; in real cases you usually get a 'const char *fname' function-parameter, without any limit for the length.
 
Old 09-12-2012, 01:02 PM   #20
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by Celyr View Post
the filename array is 128 long. So buffer overflow is impossible
Quote:
Originally Posted by Celyr View Post
Look how many nested if I had to add! That's just lol
One of the major causes of bad programming is focusing on just the exact current situation instead of considering the likely evolution of similar situations.

That is especially bad where it leads to beginner programming styles that scale up into land mines for real size projects. I think that applies to both the styles that Celyr is advocating in these comments.

Quote:
Originally Posted by NevemTeve View Post
2. free (NULL) might be problematic on some old/exotic platforms, so it is wise to make a habit of checking
Another major cause of bad programming is over generalizing to cover theoretical issues that will never arise in the evolution of the current work. Excess code distracts from the smooth flow of the necessary code and increases the probability of bugs.

Any of those old/exotic platforms support vasprintf ?

No two experts will agree on the exact boundary between properly generalizing your code and over generalizing your code. But, the issues are real, not just a matter of opinion. Putting that boundary in a worse place results in worse programming.
 
1 members found this post helpful.
Old 09-12-2012, 01:44 PM   #21
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I'm surprised no one has brought up the solution that removes the requirement for sprintf() + system(). If all of the arguments are already present, and you just want to launch an editor, then why not use the arguments in a call to one of the exec() (following fork(), of course) system call family? This has the efficiency gain of not loading a shell to load an editor, when all you really wanted was the editor.
I find it interesting how often this is done, as well as how often system() is used to accomplish that which can be done by the language used to make the call (although this is not one of those cases).
--- rod.

Last edited by theNbomr; 09-12-2012 at 01:46 PM.
 
  


Reply

Tags
gcc, linux, script, shell, unix


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine multiple text files into one text file? dirtydog7655 Linux - Newbie 6 05-16-2013 10:47 PM
[SOLVED] Variables in text file, how to get their values when printing out the text? idaham Linux - General 2 04-14-2010 03:28 AM
Trying to combine lookahead and variable substitution djeepp Programming 1 09-03-2008 04:32 PM
int values from function main shifter Programming 6 05-07-2007 04:54 PM
how to send shell variable data to a text file ginda Programming 7 06-23-2006 05:49 AM


All times are GMT -5. The time now is 12:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration