 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Vinay Guest
|
Posted: Tue May 15, 2007 9:06 pm Post subject: String length of command line arguments |
|
|
Hello guys,
My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
ex - a.out abcdefghij......................................
What can be the length of that command line argument.
I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
Thanks
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Barry Schwarz Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
On 15 May 2007 16:06:33 GMT, Vinay <samurai1269 (AT) gmail (DOT) com> wrote:
| Quote: | Hello guys,
My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
ex - a.out abcdefghij......................................
What can be the length of that command line argument.
|
The lower of the two limits:
What your C implementation can support (should be documented
somewhere) .
What your operating system can support (should also be
documented somewhere.
| Quote: |
I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
|
Why are you copying it?
Do you really want to waste all the resources to handle the largest
possible argument (which will probably be in the thousands of
characters) when it is very unlikely you will ever see anything that
long. If you really must copy it, dynamically allocate
strlen(argv[1])+1 bytes and use that as your buffer.
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Jonathan Leffler Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
Vinay wrote:
| Quote: | My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
ex - a.out abcdefghij......................................
What can be the length of that command line argument.
I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
|
It varies, by platform and the environment.
I just had to chop a program that worked OK on Solaris with 128 KB down
to 64 KB on Linux to get it to work - 64 KB was the easy number to try
when 128 KB failed.
--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler (AT) earthlink (DOT) net, jleffler (AT) us (DOT) ibm.com
Guardian of DBD::Informix v2007.0226 -- http://dbi.perl.org/
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
Vinay wrote:
| Quote: | My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
|
That depends on the platform. It is best not to assume any particular limit.
| Quote: | I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
|
So don't do it that way. Note that argv[i] is a pointer to char,
which occupies very little space. You can probably work with that
pointer directly. If you do need to allocate storage to hold the
pointed-to string, use malloc (and check that it succeeds).
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Gordon Burditt Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
| Quote: | My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
|
You pretty much have to assume that it could be near-infinite (e.g.
the maximum value of a signed 64-bit number, at least for a few
years). In the age of viruses and malware, someone's eventually
going to try handing just about any bad argument imaginable to your
program.
| Quote: | ex - a.out abcdefghij......................................
What can be the length of that command line argument.
|
Too Long (tm).
| Quote: | I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
|
Then check the length before copying. Or don't use a statically
allocated buffer. Or be prepared for trouble when you have to
release Service Pack 65536 to fix this, and that just overflowed
the 16-bit unsigned number you use to count service packs.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
On 15 May 2007 16:06:33 GMT, Vinay <samurai1269 (AT) gmail (DOT) com> wrote in
comp.lang.c.moderated:
| Quote: | Hello guys,
My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
ex - a.out abcdefghij......................................
What can be the length of that command line argument.
|
The C standard does not specify a maximum length for command line
arguments.
| Quote: | I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
|
This is entirely platform specific. On some platforms, it is quite
possible for a command line argument to be thousands of bytes.
There are two possible approaches:
1. Make your buffer the size of the largest string you are willing to
accept. At program start up, use strlen() to get the length of argv
[1]. If it is larger than your buffer, terminate the program with an
error message to the user.
2. If you are not allowed to set a maximum acceptable size, don't
allocate the buffer statically. Use strlen() to get the length and
then use malloc() to allocate length + 1 bytes.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Cumaar Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
Hi Vinay,
As far as the command line argument is concerned it
thing the max. length of that array should that size of the integer
type that particular system (compiler allots)
Support the integer is of 32bits then (2^31)-1
This is because the signature says
int main(int argc, char *argv[])
{}
So if argc is (2^31)-1 you have to provide as many arguments.
If i am wrong kindly correct me.
Cheers,
Kumaar Guhan
On May 15, 9:06 pm, Vinay <samurai1...@gmail.com> wrote:
| Quote: | Hello guys,
My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
ex - a.out abcdefghij......................................
What can be the length of that command line argument.
I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
Thanks
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-- |
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Hans-Bernhard Bröker Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
Vinay wrote:
| Quote: | My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
|
As long as it happens to be. strlen() tells you how long that is.
| Quote: | I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
|
So don't do that then. Use a dynamically allocated buffer, re-think if
you really need to copy the argument in order to use it, or just refuse
to continue if the argument is too long.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
John Dallman Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
In article <clcm-20070515-0005 (AT) plethora (DOT) net>, samurai1269 (AT) gmail (DOT) com
(Vinay) wrote:
| Quote: | My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
|
This is completely dependent on the operating system you're running on,
and quite possibly on other things within that operating system (e.g.,
on MS-Windows, I seem to remember that COMMAND.COM and CMD.EXE have
different limits).
If your code is going to try to handle such things portably, a static
buffer is the wrong way to do it: use strlen() to find out how big the
arguments are and allocate space for them dynamically.
--
John Dallman, jgd (AT) cix (DOT) co.uk, HTML mail is treated as probable spam.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Clark Cox Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
On 2007-05-15 09:06:33 -0700, Vinay <samurai1269 (AT) gmail (DOT) com> said:
| Quote: | Hello guys,
My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
ex - a.out abcdefghij......................................
What can be the length of that command line argument.
|
It can be whatever your platform allows it to be. C really doesn't care.
| Quote: | I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
|
Well, check the length of the argument before copying to the buffer.
Or, use a dynamically allocated buffer of the proper size, and be sure
to check for NULL return values from malloc and friends.
--
Clark S. Cox III
clarkcox3 (AT) gmail (DOT) com
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
WillerZ Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
Vinay wrote:
| Quote: | Hello guys,
My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
ex - a.out abcdefghij......................................
What can be the length of that command line argument.
|
System dependant. I've seen up to 32K.
| Quote: | I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
|
Don't do that then. Use dynamic allocation or, even better, avoid the
need to copy the arguments in the first place.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Allan Chandler Guest
|
Posted: Fri May 18, 2007 9:11 am Post subject: Re: String length of command line arguments |
|
|
Hans-Bernhard Bröker wrote:
| Quote: | Vinay wrote:
My question is when we are kicking off a program that expects some
arguments ( command line args ), How long can the argument be?
As long as it happens to be. strlen() tells you how long that is.
I need this because if I am trying to copy argv[1] into a statically
allocated buffer it might cause a buffer overflow if I have big
command line argument.
So don't do that then. Use a dynamically allocated buffer, re-think if
you really need to copy the argument in order to use it, or just refuse
to continue if the argument is too long.
|
Okay, I've seen plenty of messages suggesting how to do it but none
giving a concrete example, which I've always thought would help newbies.
I would try something along the lines of:
int main (int argc, char *argv[]) {
char *argv1copy;
if (argc < 2) {
fprintf (stderr, "Not enough args\n");
return 1;
}
argv1copy = malloc (strlen (argv[1]) + 1);
if (argv1copy == NULL) {
fprintf (stderr, "Cannot allocate memory\n");
return 1;
}
strcpy (argv1copy, argv[1]);
/* Do whatever you have to with argv1copy */
free (argv1copy);
return 0;
}
Most of the posts have been correct on one thing.
Unless you need to change the string passed in, you're better off using
it where it is rather than making a copy.
You can simply put
argv1copy = argv[1];
in you code to achieve this.
Cheers,
Pax
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|