From 8f5dbf0ace53daf0b2b4d48f2c5151000f0d5720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Mon, 28 Mar 2011 13:42:58 +0200 Subject: Adding comments; variable scoping There should be no semantic differences, otherwise. Reviewed-by: Harald Fernengel Reviewed-by: Robin Burchell --- src/corelib/io/qtemporaryfile.cpp | 52 ++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index a9bed8b..939ea34 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -102,11 +102,6 @@ QT_BEGIN_NAMESPACE static int _gettemp(char *path, int slen) { char *start, *trv, *suffp; -#if defined(Q_OS_WIN) - int pid; -#else - pid_t pid; -#endif for (trv = path; *trv; ++trv) ; @@ -117,32 +112,43 @@ static int _gettemp(char *path, int slen) errno = EINVAL; return -1; } + + // Initialize placeholder with random chars + PID. + { +#if defined(Q_OS_WIN) + int pid; +#else + pid_t pid; +#endif + #if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400 - pid = _getpid(); + pid = _getpid(); #elif defined(Q_OS_VXWORKS) - pid = (pid_t) taskIdCurrent; + pid = (pid_t) taskIdCurrent; #else - pid = getpid(); + pid = getpid(); #endif - while (trv >= path && *trv == 'X' && pid != 0) { - *trv-- = (pid % 10) + '0'; - pid /= 10; - } + while (trv >= path && *trv == 'X' && pid != 0) { + *trv-- = (pid % 10) + '0'; + pid /= 10; + } - while (trv >= path && *trv == 'X') { - char c; + while (trv >= path && *trv == 'X') { + char c; - // CHANGE arc4random() -> random() - pid = (qrand() & 0xffff) % (26+26); - if (pid < 26) - c = pid + 'A'; - else - c = (pid - 26) + 'a'; - *trv-- = c; + // CHANGE arc4random() -> random() + int pid = (qrand() & 0xffff) % (26+26); + if (pid < 26) + c = pid + 'A'; + else + c = (pid - 26) + 'a'; + *trv-- = c; + } + start = trv + 1; } - start = trv + 1; for (;;) { + // Atomically create file and obtain handle #ifndef Q_OS_WIN { int fd = QT_OPEN(path, QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE, 0600); @@ -158,6 +164,8 @@ static int _gettemp(char *path, int slen) /* tricky little algorwwithm for backward compatibility */ for (trv = start;;) { + // Character progression: [0-9] => 'a' ... 'z' => 'A' .. 'Z' + // String progression: "ZZaiC" => "aabiC" if (!*trv) return -1; if (*trv == 'Z') { -- cgit v0.12