The codec for string from qgetenv and pw->pw_name is not necessarily utf8. So use QString::fromLocal8Bit and QStrings.

This commit is contained in:
Leander Schulten 2020-03-27 07:52:48 +01:00
parent 4abe20afbf
commit 49c282a64c
2 changed files with 11 additions and 10 deletions

View File

@ -84,23 +84,24 @@ SingleApplicationPrivate::~SingleApplicationPrivate()
delete memory; delete memory;
} }
QByteArray SingleApplicationPrivate::getUsername(){ QString SingleApplicationPrivate::getUsername()
{
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
wchar_t username[UNLEN + 1]; wchar_t username[UNLEN + 1];
// Specifies size of the buffer on input // Specifies size of the buffer on input
DWORD usernameLength = UNLEN + 1; DWORD usernameLength = UNLEN + 1;
if( GetUserNameW( username, &usernameLength ) ) if( GetUserNameW( username, &usernameLength ) )
return QString::fromWCharArray( username ).toUtf8(); return QString::fromWCharArray( username );
return qgetenv( "USERNAME" ); return qEnvironmentVariable( "USERNAME" );
#endif #endif
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
QByteArray username; QString username;
uid_t uid = geteuid(); uid_t uid = geteuid();
struct passwd *pw = getpwuid( uid ); struct passwd *pw = getpwuid( uid );
if( pw ) if( pw )
username = pw->pw_name; username = QString::fromLocal8Bit( pw->pw_name );
if( username.isEmpty() ) if( username.isEmpty() )
username = qgetenv( "USER" ); username = qEnvironmentVariable( "USER" );
return username; return username;
#endif #endif
} }
@ -127,7 +128,7 @@ void SingleApplicationPrivate::genBlockServerName()
// User level block requires a user specific data in the hash // User level block requires a user specific data in the hash
if( options & SingleApplication::Mode::User ) { if( options & SingleApplication::Mode::User ) {
appData.addData( getUsername() ); appData.addData( getUsername().toUtf8() );
} }
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
@ -175,7 +176,7 @@ void SingleApplicationPrivate::startPrimary()
inst->primary = true; inst->primary = true;
inst->primaryPid = q->applicationPid(); inst->primaryPid = q->applicationPid();
strncpy( inst->primaryUser, getUsername().data(), 127 ); strncpy( inst->primaryUser, getUsername().toUtf8().data(), 127 );
inst->primaryUser[127] = '\0'; inst->primaryUser[127] = '\0';
inst->checksum = blockChecksum(); inst->checksum = blockChecksum();

View File

@ -72,7 +72,7 @@ public:
SingleApplicationPrivate( SingleApplication *q_ptr ); SingleApplicationPrivate( SingleApplication *q_ptr );
~SingleApplicationPrivate(); ~SingleApplicationPrivate();
QByteArray getUsername(); QString getUsername();
void genBlockServerName(); void genBlockServerName();
void initializeMemoryBlock(); void initializeMemoryBlock();
void startPrimary(); void startPrimary();