Removed GetSpecialFolderPath() fallbacks (as requested), removed several unneeded spaces, renamed MyGetSpecialFolderPath() -> GetSpecialFolderPath() as the first one sounds not very pro ^^, make sure all char arrays in util.cpp get initialized to 0 and use sizeof(CharArray) instead of MAX_LEN, renamed Windows Autostart shortcut to "Bitcoin-Qt.lnk".
Updates / cleanup for utility functions #1066
pull Diapolo wants to merge 3 commits into bitcoin:master from Diapolo:util-updates changing 4 files +108 −116-
Diapolo commented at 10:40 PM on April 8, 2012: none
-
in src/util.cpp:None in 7256374bb5 outdated
787 | } 788 | - else if (nFolder == CSIDL_APPDATA) 789 | + else if (nFolder == CSIDL_STARTUP) 790 | { 791 | - return getenv("APPDATA"); 792 | + // Windows < Vista: C:\Documents and Settings\Username\StartMenu\Programs\Startup
sipa commented at 10:58 PM on April 8, 2012:Is there no way of asking windows for the startup folder, in a version-agnostic way? Seems hard to believe one would need guesswork like this.
Diapolo commented at 11:03 PM on April 8, 2012:If SHGetSpecialFolderPathA() fails we HAVE to guess (our fallback) or GetSpecialFolderPath() fails ... there is NO environment variable that points to the autostart folder and MS changed the folder in Vista and higher in comparison to i.e. WinXP.
laanwj commented at 8:42 AM on April 9, 2012:I'd prefer simply giving back an error, instead of guessing and fooling around. I suppose if GetSpecialFolderPath fails something is wrong anyway.
Diapolo commented at 9:06 AM on April 9, 2012:The fallback WAS in before I edited the file, but it would have failed for Vista and higher. If you are fine with the rest of the changes I can remove this, sure.
laanwj commented at 9:13 AM on April 9, 2012:I think it's fine to change it as you did. But I'd also be fine with removing the fallback and returning error.
There are places where extensive recovery mechanisms for OS errors are warranted, but this isn't one of them (how likely is requesting a string from the OS to fail?). No need to spend much time on this.
in src/util.cpp:None in 496838e2da outdated
163 | @@ -164,7 +164,8 @@ inline int OutputDebugStringF(const char* pszFormat, ...) 164 | 165 | if (!fileout) 166 | { 167 | - char pszFile[MAX_PATH+100];
Diapolo commented at 10:30 AM on April 9, 2012:This one broke MAX_PATH limits, which seems bad.
in src/util.cpp:None in 496838e2da outdated
257 | @@ -257,9 +258,10 @@ int my_snprintf(char* buffer, size_t limit, const char* format, ...) 258 | 259 | string real_strprintf(const std::string &format, int dummy, ...) 260 | { 261 | - char buffer[50000]; 262 | - char* p = buffer; 263 | - int limit = sizeof(buffer); 264 | + char pszBuffer[50000]; 265 | + memset(pszBuffer, 0, sizeof(pszBuffer));
gavinandresen commented at 1:40 PM on April 9, 2012:How often does the code call real_strprintf()? This change means clearing 50K or memory every time it is called, which might be a performance issue if it is called a lot.
Diapolo commented at 1:44 PM on April 9, 2012:I can check, but I think it's good style to init used arrays or variables, as this creates a clean environment for code relying on that array. What's your oppinion here?
gavinandresen commented at 2:02 PM on April 9, 2012:Please check. It is absolutely good style, but if it make the GUI super sluggish because we're calling it 100 times per second when computing the list of transactions to show then performance trumps style (I have no idea if we are or not, but don't want people to start complaining that the 0.6.1 release is a lot slower than 0.6....)
laanwj commented at 2:10 PM on April 9, 2012:real_strprintf is called a lot of times in many places (for example, HexStr).
I don't think clearing it every time adds anything. The buffer is only written by _vsnprintf, which has well-defined semantics and doesn't do anything with the contents of the buffer beforehand. It returns the number of characters that will be in the output string. Nothing in the strprintf function relies on zero bytes anywhere.
Diapolo commented at 2:42 PM on April 9, 2012:If real_strprintf is "safe" and you are right, it's quite often called ... I'm fine with removing the init. In general my intention was to reduce the risk of buffer-overflows / buffer-corruptions causing weird things to happen. It would be nice, if we were able to rewrite funtcions to use std::string instead of char * or char arrays.
laanwj commented at 2:52 PM on April 9, 2012:It would be nice, if we were able to rewrite funtcions to use std::string instead of char * or char arrays
We certainly agree on that. Apart from places where you really can't get around it such as strprintf, which wraps a legacy C function, there should be as few fixed-size buffers used as possible.
Diapolo commented at 4:02 PM on April 9, 2012:Removed the memset() in the last rebase!
in src/util.cpp:None in 496838e2da outdated
287 | 288 | bool error(const char *format, ...) 289 | { 290 | - char buffer[50000]; 291 | - int limit = sizeof(buffer); 292 | + char pszBuffer[50000];
laanwj commented at 2:10 PM on April 9, 2012:Why rename this variable,
bufferis a fine name?
sipa commented at 2:40 PM on April 9, 2012:I suppose he's following the code guidelines - you know, the contents of doc/coding.txt? ;)
Diapolo commented at 2:40 PM on April 9, 2012:Variable names without a prefix break coding style standard I think ... somethimes I really would like to have mandantory rules, that I can use while coding. I don't insist on that rename, but if you use buffer, you simply don't know what type it is.
laanwj commented at 2:50 PM on April 9, 2012:Can't say I really like this guideline. Encoding types into variable names reeks of the windows "Hungarian notation" fad, and makes little sense to me. IMO, variable names should be clear and human readable. My IDE and compiler figure out the rest from the context.
Then again, if you think this makes the util code more consistent feel free to do it here. Please don't do it throughout the qt code though.
sipa commented at 2:52 PM on April 9, 2012:I don't think many people except for Satoshi like this coding standard, actually, but I must say I like the consistency.
That said, I never realized it wasn't used in the Qt code...
laanwj commented at 2:55 PM on April 9, 2012:Eventually it makes no sense in C++ anyway, as there are so many types and classes.
Diapolo commented at 4:02 PM on April 9, 2012:Reverted to buffer and will keep using this ^^.
in src/util.cpp:None in 496838e2da outdated
257 | @@ -257,9 +258,10 @@ int my_snprintf(char* buffer, size_t limit, const char* format, ...) 258 | 259 | string real_strprintf(const std::string &format, int dummy, ...) 260 | { 261 | - char buffer[50000]; 262 | - char* p = buffer; 263 | - int limit = sizeof(buffer); 264 | + char pszBuffer[50000];
laanwj commented at 2:11 PM on April 9, 2012:Why "pszBuffer"?
Diapolo commented at 4:03 PM on April 9, 2012:Here it's buffer again, too ;).
in src/init.cpp:None in 2aedf05fc3 outdated
582 | @@ -583,7 +583,7 @@ bool AppInit2(int argc, char* argv[]) 583 | #ifdef WIN32 584 | string StartupShortcutPath() 585 | { 586 | - return MyGetSpecialFolderPath(CSIDL_STARTUP, true) + "\\Bitcoin.lnk"; 587 | + return GetSpecialFolderPath(CSIDL_STARTUP) + "\\Bitcoin.lnk";
luke-jr commented at 9:11 PM on April 9, 2012:Shouldn't this probably be Bitcoin-Qt.lnk anyway?
Diapolo commented at 9:20 PM on April 9, 2012:That's a nice suggestion, perhaps delete the old one if it exists and create a new Bitcoin-Qt.lnk.
laanwj commented at 5:49 AM on April 10, 2012:Does the name of the file matter here? It'll just be overwritten with a correct link right now. I don't think there a pressing need to add rename+delete logic.
in src/util.cpp:None in 2aedf05fc3 outdated
21 | @@ -22,7 +22,7 @@ 22 | bool fDebug = false; 23 | bool fPrintToConsole = false; 24 | bool fPrintToDebugger = false; 25 | -char pszSetDataDir[MAX_PATH] = ""; 26 | +char pszSetDataDir[MAX_PATH];
luke-jr commented at 9:12 PM on April 9, 2012:This introduces a bug. Uninitialized char arrays are not guaranteed to be null.
Diapolo commented at 9:19 PM on April 9, 2012:Take a look into ParseParameters() init happens there via memset.
luke-jr commented at 9:37 PM on April 9, 2012:It's only memset if -datadir is specified...
Diapolo commented at 9:42 PM on April 9, 2012:Alright, that can be changed and thanks :-D.
Diapolo commented at 9:58 PM on April 9, 2012:I re-added this ;).
laanwj commented at 5:45 AM on April 10, 2012:See my other remark. Please keep it at ="" or ={0} (equivalent), which does the same in ~5% of the characters.
in src/util.cpp:None in 8ba33eed32 outdated
164 | @@ -165,8 +165,9 @@ inline int OutputDebugStringF(const char* pszFormat, ...) 165 | 166 | if (!fileout) 167 | { 168 | - char pszFile[MAX_PATH+100]; 169 | - GetDataDir(pszFile); 170 | + char pszFile[MAX_PATH]; 171 | + memset(pszFile, 0, sizeof(pszFile));
luke-jr commented at 9:16 PM on April 9, 2012:Is there a need to initialize pszFile/etc to null?
Diapolo commented at 9:20 PM on April 9, 2012:Good coding style IMHO.
luke-jr commented at 9:39 PM on April 9, 2012:Bad coding style, IMHO.
laanwj commented at 5:34 AM on April 10, 2012:I am very sure (but I don't have time to check with the C standard right now) that ="" is the same as ={0}; and does exactly the same as memset: initialize the array to zeros. No security theater please, I'd like to keep it at "" which is clearest to developers.
Diapolo commented at 6:42 AM on April 10, 2012:@laanwj You convinced me, the important part is not via which method an array is initialized as long as we just initialize it :).
from http://www.cplusplus.com/doc/tutorial/arrays/ "When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros."
Diapolo commented at 7:08 AM on April 10, 2012: noneLast commit reverts to old style of array init via = "", which is allowed and shorter than a memset(). At least now all arrays are initialized :). The only exception is in real_strprintf() where the init could perhaps slow down the GUI / client.
in src/util.cpp:None in 270f11d8db outdated
209 | - int limit = END(pszBuffer) - pend - 2; 210 | + int limit = END(staticBuffer) - pend - 2; 211 | int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr); 212 | va_end(arg_ptr); 213 | if (ret < 0 || ret >= limit) 214 | {
laanwj commented at 7:13 AM on April 10, 2012:I wonder if we couldn't reduce all this wonderfully verbose code to about two lines of C++ string manipulation? :-) (or even better, reuse
strprintf? At least by adding an intermediate functionvstrprintfthat takes a va_arg list) Edit: doesn't necessarily need to be in this patch though, just a comment in general
Diapolo commented at 7:19 AM on April 10, 2012:Every simplyfication is a great step, so I agree ... but I don't want to bloat this pull request :). I would like sipas boost pathm rework merged into master before my util updates get in, as he has a better GetDataDir() version.
laanwj commented at 7:13 AM on April 10, 2012: memberThanks, looking good now.
luke-jr commented at 1:54 PM on April 10, 2012: memberRebasing required.
in src/util.cpp:None in 270f11d8db outdated
732 | - pszModule[0] = '\0'; 733 | - GetModuleFileNameA(NULL, pszModule, sizeof(pszModule)); 734 | + char module[MAX_PATH] = ""; 735 | + GetModuleFileNameA(NULL, module, sizeof(module)); 736 | #else 737 | const char* pszModule = "bitcoin";
luke-jr commented at 4:58 PM on April 10, 2012:If you're going to rename pszModule, you need to do it here too...
Diapolo commented at 5:43 PM on April 10, 2012:Fixed, that went through because I used a Refactoring function, which didn't catch this ;). Thank you for watching.
Diapolo commented at 4:59 AM on April 12, 2012: noneNice, will rebase later today :).
Diapolo commented at 6:57 AM on April 12, 2012: noneRebased and I'm looking through the code again to verify...
Updates / cleanup for utility functions 2f1f65430erename Windows Autostart shortcut to "Bitcoin-Qt.lnk" and ensure the legacy shortcut gets updated dc21c2929aremoved an unused var that I overlooked while resolving rebase conflicts 081ba7fd34in src/util.cpp:None in d17a91f7de outdated
195 | @@ -196,23 +196,24 @@ inline int OutputDebugStringF(const char* pszFormat, ...) 196 | { 197 | LOCK(cs_OutputDebugStringF); 198 | static char pszBuffer[50000];
sipa commented at 2:24 PM on April 12, 2012:Is pszBuffer still used now?
Diapolo commented at 4:06 PM on April 12, 2012:That must be a merge misstake I overlooked ... removed :), thanks for your hint!
Diapolo commented at 4:45 PM on April 12, 2012: noneRebased one more ;-). I'm only going to fix glitches / errors that are currently in my changes, as I don't want to bloat this. There is of course plenty of room for optimizing our utility functions, but that should go to seperate pull requests.
luke-jr commented at 9:10 PM on April 12, 2012: memberMost of this looks like it makes the code more unreadable. IMO, the absolute namespaces are far clearer, and should be encouraged, while the general "using namespace" discouraged. sipa's per-function "using namespace foo = boost::longerfoo" makes sense and doesn't interfere with readability.
Diapolo commented at 9:28 PM on April 12, 2012: noneGod damn ... everytime I try to harmonize the code someone has an own feeling about things. BUT most of the time I see things in the code + in many places and use these as base. I have no problem to completely discard the global use of "using namespace" if everyone would do it. Function local using namespaces seem much more weird to me.
But even if you don't like the style ... there are at least valuable fixes in here, like init buffers that were not before, renaming Bitcoin.lnk to Bitcoin-Qt.lnk and the removal of a fallback for a Windows function that sipa suggested. The whole work on filesystem::path was obsolete as Sipa did that (and yes even better then I could).
sipa commented at 10:56 PM on April 12, 2012: memberI really don't care about namespaces, and only followed the pattern of the code already there when doing the boost::filesystem::path stuff. As long as there are no namespaces in header files, I don't care.
gavinandresen commented at 11:19 PM on April 12, 2012: contributor56 comments for such trivial changes... I think there are higher priority things we could be spending our time on.
This is why I don't like "I'll just clean up the code because I can" changes. I would much prefer to see code cleaned up as it is being improved, so:
- Fix a real bug (we have 180 of them, there are plenty to choose from), and do a little code cleanup as part of the fix.
- Add some unit tests, and clean up the code you are unit-testing (make sure you run the unit tests both before and after the cleanup).
- Add a new feature, and clean up any code that has to be changed to support the new feature.
Diapolo: we have to weigh the benefits of "cleaner" code against the potential costs of ANY changes to the code. It is incredibly easy to screw up and let one tiny little change through that has a really bad unintended consequence.
freewil commented at 12:12 AM on April 13, 2012: contributorMay I suggest for code cleanup pull requests, you make a smaller and simpler pull request for each one such as "make sure all char arrays in util.cpp get initialized to 0 and use sizeof(CharArray) instead of MAX_LEN"
laanwj commented at 5:58 AM on April 13, 2012: memberI like bold code cleanups, such as code de-duplication, eliminating unneeded functions, replacing archaic and dangerous C with modern C++, grouping together similar functionality, isolating different concerns, untangling knots of unrelated include files, and so on.
Oh yes and: fixing compiler warnings! Those boatloads of warnings are a shame to the project.
On the other hand changing namespace references, or renaming local variables, as you see it causes a lot of bikeshedding discussion for no real good. yes, everyone has their own feeling about such things. It's better to harmonize people than harmonize code in this case, and just leave it be...
Combining bug fixes with a little related cleanup (as you did with the QR patch) is great though. It's nice to not be the only person that works on the UI.
Diapolo commented at 9:46 AM on April 13, 2012: noneAllright, 2 options for me ... leave this as it is or rework to change no var names and only add the small changes I did code-wise. First vore wins, let's go :D.
Edit: @laanwj Thanks, I'm more the GUI lover than console user and it's fun to work together :).
gavinandresen commented at 11:57 PM on April 13, 2012: contributorFirst vote: rework.
gavinandresen closed this on Apr 13, 2012Diapolo commented at 1:13 PM on April 21, 2012: noneI'm back and will do this in the next days :) and btw. I like clear orders ^^.
suprnurd referenced this in commit 342bda5fdc on Dec 5, 2017sanch0panza referenced this in commit ce467731de on May 17, 2018lateminer referenced this in commit 6a4bf7c42c on Nov 14, 2019dexX7 referenced this in commit 8d7b553a58 on Jan 18, 2020DrahtBot locked this on Sep 8, 2021
github-metadata-mirror
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-19 12:16 UTC
This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me