Line 0
Link Here
|
|
|
1 |
Upstream PR: https://github.com/s3fs-fuse/s3fs-fuse/pull/729 |
2 |
--- src/s3fs_util.cpp.orig 2017-12-17 07:53:49 UTC |
3 |
+++ src/s3fs_util.cpp |
4 |
@@ -453,6 +453,7 @@ AutoLock::~AutoLock() |
5 |
string get_username(uid_t uid) |
6 |
{ |
7 |
static size_t maxlen = 0; // set once |
8 |
+ int result; |
9 |
char* pbuf; |
10 |
struct passwd pwinfo; |
11 |
struct passwd* ppwinfo = NULL; |
12 |
@@ -461,9 +462,17 @@ string get_username(uid_t uid) |
13 |
if(0 == maxlen){ |
14 |
long res = sysconf(_SC_GETPW_R_SIZE_MAX); |
15 |
if(0 > res){ |
16 |
- S3FS_PRN_WARN("could not get max pw length."); |
17 |
- maxlen = 0; |
18 |
- return string(""); |
19 |
+ // SUSv4tc1 says the following about _SC_GETGR_R_SIZE_MAX and |
20 |
+ // _SC_GETPW_R_SIZE_MAX: |
21 |
+ // Note that sysconf(_SC_GETGR_R_SIZE_MAX) may return -1 if |
22 |
+ // there is no hard limit on the size of the buffer needed to |
23 |
+ // store all the groups returned. |
24 |
+ if (errno != 0){ |
25 |
+ S3FS_PRN_WARN("could not get max pw length."); |
26 |
+ maxlen = 0; |
27 |
+ return string(""); |
28 |
+ } |
29 |
+ res = 1024; // default initial length |
30 |
} |
31 |
maxlen = res; |
32 |
} |
33 |
@@ -471,12 +480,22 @@ string get_username(uid_t uid) |
34 |
S3FS_PRN_CRIT("failed to allocate memory."); |
35 |
return string(""); |
36 |
} |
37 |
- // get group information |
38 |
- if(0 != getpwuid_r(uid, &pwinfo, pbuf, maxlen, &ppwinfo)){ |
39 |
- S3FS_PRN_WARN("could not get pw information."); |
40 |
+ // get pw information |
41 |
+ while(ERANGE == (result = getpwuid_r(uid, &pwinfo, pbuf, maxlen, &ppwinfo))){ |
42 |
+ free(pbuf); |
43 |
+ maxlen *= 2; |
44 |
+ if(NULL == (pbuf = (char*)malloc(sizeof(char) * maxlen))){ |
45 |
+ S3FS_PRN_CRIT("failed to allocate memory."); |
46 |
+ return string(""); |
47 |
+ } |
48 |
+ } |
49 |
+ |
50 |
+ if(0 != result){ |
51 |
+ S3FS_PRN_ERR("could not get pw information(%d).", result); |
52 |
free(pbuf); |
53 |
return string(""); |
54 |
} |
55 |
+ |
56 |
// check pw |
57 |
if(NULL == ppwinfo){ |
58 |
free(pbuf); |
59 |
@@ -498,10 +517,18 @@ int is_uid_include_group(uid_t uid, gid_ |
60 |
// make buffer |
61 |
if(0 == maxlen){ |
62 |
long res = sysconf(_SC_GETGR_R_SIZE_MAX); |
63 |
- if(0 > res){ |
64 |
- S3FS_PRN_ERR("could not get max name length."); |
65 |
- maxlen = 0; |
66 |
- return -ERANGE; |
67 |
+ if(0 > res) { |
68 |
+ // SUSv4tc1 says the following about _SC_GETGR_R_SIZE_MAX and |
69 |
+ // _SC_GETPW_R_SIZE_MAX: |
70 |
+ // Note that sysconf(_SC_GETGR_R_SIZE_MAX) may return -1 if |
71 |
+ // there is no hard limit on the size of the buffer needed to |
72 |
+ // store all the groups returned. |
73 |
+ if (errno != 0) { |
74 |
+ S3FS_PRN_ERR("could not get max name length."); |
75 |
+ maxlen = 0; |
76 |
+ return -ERANGE; |
77 |
+ } |
78 |
+ res = 1024; // default initial length |
79 |
} |
80 |
maxlen = res; |
81 |
} |