File tree Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Original file line number Diff line number Diff line change 14
14
#include " flang/Runtime/descriptor.h"
15
15
#include < cstdlib>
16
16
#include < limits>
17
+ #include < string.h>
17
18
18
19
#ifdef _WIN32
19
20
#define WIN32_LEAN_AND_MEAN
20
21
#define NOMINMAX
21
22
#include < windows.h>
22
23
23
- #include < Lmcons.h> // UNLEN=256
24
+ #include < lmcons.h> // UNLEN=256
25
+ #include < wchar.h> // wchar_t cast to LPWSTR
26
+ #pragma comment(lib, "Advapi32.lib") // Link Advapi32.lib for GetUserName
24
27
25
- inline char *getlogin () {
26
- char * username = NULL ;
27
- DWORD size = UNLEN + 1 ; // Constant for the maximum username length
28
- username = ( char *) malloc (size );
28
+ static inline char *getlogin () {
29
+ static char username[UNLEN + 1 ] ;
30
+ wchar_t w_username[ UNLEN + 1 ];
31
+ DWORD namelen = sizeof (w_username) / sizeof (w_username[ 0 ] );
29
32
30
- if (GetUserName (username, &size)) {
31
- // Username retrieved successfully
32
- return username;
33
+ if (GetUserName (w_username, &namelen)) {
34
+ // Convert the wchar_t string to a regular C string
35
+ if (wcstombs (username, w_username, UNLEN + 1 ) == -1 ) {
36
+ // Conversion failed
37
+ return NULL ;
38
+ }
39
+ return (username[0 ] == 0 ? NULL : username);
33
40
} else {
34
- free (username);
35
41
return NULL ;
36
42
}
43
+ return nullptr ;
37
44
}
38
45
#else
39
46
#include < unistd.h>
Original file line number Diff line number Diff line change @@ -297,7 +297,7 @@ TEST_F(SeveralArguments, ArgValueTooShort) {
297
297
ASSERT_NE (tooShort, nullptr );
298
298
EXPECT_EQ (RTNAME (GetCommandArgument)(1 , tooShort.get ()), -1 );
299
299
CheckDescriptorEqStr (tooShort.get (), severalArgsArgv[1 ]);
300
-
300
+
301
301
OwningPtr<Descriptor> length{EmptyIntDescriptor ()};
302
302
ASSERT_NE (length, nullptr );
303
303
OwningPtr<Descriptor> errMsg{CreateEmptyCharDescriptor ()};
You can’t perform that action at this time.
0 commit comments