Bug 362652

Summary: unnecessary getenv call
Product: [Frameworks and Libraries] Akonadi Reporter: Markus Raab <debian>
Component: serverAssignee: kdepim bugs <kdepim-bugs>
Status: RESOLVED FIXED    
Severity: minor CC: dvratil
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In: 16.04.2

Description Markus Raab 2016-05-04 08:22:33 UTC
src/server/utils.cpp in function Utils::preferredSocketDirectory calls getenv even in cases when the userName actually is not needed.

  const QString userName = QString::fromLocal8Bit( qgetenv( "USER" ) );
  if ( socketDir.contains( QLatin1String( "$USER" ) ) && !userName.isEmpty() ) {
    socketDir.replace( QLatin1String( "$USER" ), userName );
  }


Reproducible: Always


Actual Results:  
getenv is called every time, even if no $USER should be resolved.

Expected Results:  
No call to getenv if not needed. It is also questionable if we want to keep $USER literally if USER was not found in environment.

diff --git a/src/server/utils.cpp b/src/server/utils.cpp
index 46d5063..5268f9d 100644
--- a/src/server/utils.cpp
+++ b/src/server/utils.cpp
@@ -72,9 +72,11 @@ QString Utils::preferredSocketDirectory(const QString &defaultDirectory)
         socketDir = serverSettings.value(QStringLiteral("Connection/SocketDirectory"), defaultDirectory).toString();
     }
 
-    const QString userName = QString::fromLocal8Bit(qgetenv("USER"));
-    if (socketDir.contains(QLatin1String("$USER")) && !userName.isEmpty()) {
-        socketDir.replace(QLatin1String("$USER"), userName);
+    if (socketDir.contains(QLatin1String("$USER"))) {
+        const QString userName = QString::fromLocal8Bit(qgetenv("USER"));
+        if (!userName.isEmpty()) {
+          socketDir.replace(QLatin1String("$USER"), userName);
+        }
     }
 
     if (socketDir[0] != QLatin1Char('/')) {
Comment 1 Daniel Vrátil 2016-05-15 23:17:09 UTC
Git commit c61d36c109c3749c66d1a3fe1486066c2d6b1875 by Daniel Vrátil, on behalf of Markus Raab.
Committed on 15/05/2016 at 22:21.
Pushed by dvratil into branch 'Applications/16.04'.

Don't call qgetenv(USER) when not needed
FIXED-IN: 16.08.2

M  +5    -3    src/server/utils.cpp

http://commits.kde.org/akonadi/c61d36c109c3749c66d1a3fe1486066c2d6b1875