Bug 362652 - unnecessary getenv call
Summary: unnecessary getenv call
Status: RESOLVED FIXED
Alias: None
Product: Akonadi
Classification: Frameworks and Libraries
Component: server (show other bugs)
Version: unspecified
Platform: Other Linux
: NOR minor
Target Milestone: ---
Assignee: kdepim bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-04 08:22 UTC by Markus Raab
Modified: 2016-05-15 23:22 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In: 16.04.2


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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