![]() |
| | #16 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #17 |
| Registered User Join Date: Jun 2008
Posts: 4
| Sorry, but this doesn't seem to be working universally... I managed to get that specific spot of code working with mkruk's solution (not making it a pointer) but it isn't working later in the code, specifically: (ident_a is a const char* that has already been successfully allocated and initialized here-- in my tests i'm setting it to abcdefghijklmnopqrstuvwxyz0123456789 because it's a good test string) Code: char call[sizeof(ident_a) + strlen("security lock-keychain .keychain >& /dev/null")];
snprintf(call, sizeof(call), "security lock-keychain %s.keychain >& /dev/null", keychain);
int results=system(call);
|
| Lasston is offline | |
| | #18 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| char call[sizeof(ident_a) + strlen("security lock-keychain .keychain >& /dev/null")]; Illegal. Size is not constant. However, you could do: char call[sizeof(ident_a) + sizeof("security lock-keychain .keychain >& /dev/null")]; The problem is that the buffer is not large enough to hold your entire string. It is only large enough to hold "security lock-keychain %s.keychain >& /dev/null" minus the "%s".
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #19 |
| Registered User Join Date: Jun 2008
Posts: 4
| OK two quickie questions about this: 1) If mkruk's idea is illegal, why does it work perfectly earlier in the code? I use Code: char rstring[11+strlen("dark__fun")];
snprintf(rstring, sizeof(rstring), "dark_%d_fun", (rand()*rand()));
2) I replaced strlen with sizeof and I'm still not getting the full string printed, it's now giving out after "z"... is there any way I can do what I'm trying to? |
| Lasston is offline | |
| | #20 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| It's true that it compiles, but that's because it's a compiler extension. In the standard, it's illegal.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #21 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #22 |
| Registered User Join Date: Jun 2008
Posts: 4
| I am trying to learn standard, unextended C right now, so I guess I'll be taking out the array. So... is there just no way to do something like this in the C language? It seems like no matter what I do I get an overflow. |
| Lasston is offline | |
| | #23 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #24 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
Naturally, this doesn't work if you have completely and utterly arbitrary inputs - in that case, you will have to find a way to calculate how much space it takes and then use malloc/free. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #25 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| >1) If mkruk's idea is illegal, why does it work perfectly earlier in the code? Code: >char call[sizeof(ident_a) + strlen("security lock-keychain .keychain >& /dev/null")];
>snprintf(call, sizeof(call), "security lock-keychain %s.keychain >& /dev/null", keychain);
>int results=system(call);
Code: char call[1 + strlen(keychain) + strlen("security lock-keychain .keychain >& /dev/null")];
snprintf(call, sizeof(call), "security lock-keychain %s.keychain >& /dev/null", keychain);
int results=system(call);
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #26 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| That's still C99. strlen() is a function. Its return value can only be evaluated at runtime. Consequentally, the compiler doesn't know how big the array is going to be, and you're using variable length arrays. To solve this problem, the first thing you need to do is figure out how many digits rand() can return. One way to do that is to look at RAND_MAX and count its digits. Code: int digits_in_number(int n) {
int digits = 0;
while(n) {
digits ++;
n /= 10;
}
return digits;
}
digits_in_number(RAND_MAX);
Note that I've created a function to figure out how much memory to allocate, so again, you'll either have to use VLAs (variable length arrays) or dynamic memory allocation. I suggest memory allocation. It's not too hard. Code: char *call = malloc(digits_in_number(ident_a)
+ strlen("security lock-keychain .keychain >& /dev/null") + 1); /* +1 for the NULL */
int result;
sprintf(call, "security lock-keychain %s.keychain >& /dev/null", keychain);
result = system(call);
free(call);
Anyway . . . hopefully that helped a little bit . . . .
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #27 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| >I'm not sure why you're adding the size of ident_a to the string, though That's supposed to be keychain. That's the only change I made from his original code, plus adding one for the string terminator.
__________________ http://www.freechess.org |
| swoopy is offline | |
![]() |
| Tags |
| buffer, malloc, overflow, random, sprintf |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Converting a circulating mouse pointer to use a Potentionmeter | phoenix23 | C Programming | 16 | 10-29-2006 05:04 AM |
| Question on buffer overflows | maxhavoc | C++ Programming | 3 | 11-25-2004 03:48 PM |
| buffer contents swapping | daluu | C++ Programming | 7 | 10-14-2004 02:34 PM |
| Avoiding Buffer Overflows | Aidman | C++ Programming | 5 | 01-03-2004 12:21 PM |
| Console Screen Buffer | GaPe | Windows Programming | 0 | 02-06-2003 05:15 AM |