@@ -79,6 +79,14 @@ static void parse_line(char *line, MountData *output) {
79
79
// output.dir: /home/netblue/.cache
80
80
// output.fstype: tmpfs
81
81
82
+ size_t linelen = strlen (line );
83
+ if (linelen >= MAX_BUF - 1 ) {
84
+ fprintf (stderr , "Error: /proc/self/mountinfo line is too long and may be truncated (%zu >= %d): %s\n" ,
85
+ linelen , MAX_BUF - 1 , line );
86
+ exit (1 );
87
+ }
88
+
89
+ char * orig_line = strdup (line );
82
90
char * ptr = strtok (line , " " );
83
91
if (!ptr )
84
92
goto errexit ;
@@ -107,17 +115,22 @@ static void parse_line(char *line, MountData *output) {
107
115
if (output -> mountid < 0 ||
108
116
output -> fsname == NULL ||
109
117
output -> dir == NULL ||
110
- output -> fstype == NULL )
118
+ output -> fstype == NULL ) {
119
+ fprintf (stderr , "Error: invalid MountData\n" );
111
120
goto errexit ;
121
+ }
112
122
113
123
// restore empty spaces
114
124
unmangle_path (output -> fsname );
115
125
unmangle_path (output -> dir );
116
126
127
+ free (orig_line );
117
128
return ;
118
129
119
130
errexit :
120
- fprintf (stderr , "Error: cannot read /proc/self/mountinfo\n" );
131
+ fprintf (stderr , "Error: cannot parse line from /proc/self/mountinfo: %s" , orig_line );
132
+ fprintf (stderr , "Error: MountData: mountid=%d fsname=%s dir=%s fstype=%s\n" ,
133
+ output -> mountid , output -> fsname , output -> dir , output -> fstype );
121
134
exit (1 );
122
135
}
123
136
@@ -127,7 +140,7 @@ MountData *get_last_mount(void) {
127
140
FILE * fp = fopen ("/proc/self/mountinfo" , "re" );
128
141
if (!fp ) {
129
142
fprintf (stderr , "Error: cannot read /proc/self/mountinfo\n" );
130
- exit ( 1 );
143
+ errExit ( "fopen" );
131
144
}
132
145
133
146
mbuf [0 ] = '\0' ;
@@ -158,8 +171,8 @@ static int get_mount_id_from_handle(int fd) {
158
171
int rv = -1 ;
159
172
int tmp ;
160
173
if (name_to_handle_at (-1 , proc , fh , & tmp , AT_SYMLINK_FOLLOW ) != -1 ) {
161
- fprintf (stderr , "Error: unexpected result from name_to_handle_at\n" );
162
- exit ( 1 );
174
+ fprintf (stderr , "Error: unexpected result from name_to_handle_at for fd %d \n" , fd );
175
+ errExit ( "name_to_handle_at" );
163
176
}
164
177
if (errno == EOVERFLOW && fh -> handle_bytes )
165
178
rv = tmp ;
@@ -184,8 +197,8 @@ static int get_mount_id_from_fdinfo(int fd) {
184
197
185
198
FILE * fp = fopen (proc , "re" );
186
199
if (!fp ) {
187
- fprintf (stderr , "Error: cannot read proc file \n" );
188
- exit ( 1 );
200
+ fprintf (stderr , "Error: cannot read %s \n" , proc );
201
+ errExit ( "fopen" );
189
202
}
190
203
191
204
if (called_as_root == 0 )
@@ -218,7 +231,7 @@ char **build_mount_array(const int mountid, const char *path) {
218
231
FILE * fp = fopen ("/proc/self/mountinfo" , "re" );
219
232
if (!fp ) {
220
233
fprintf (stderr , "Error: cannot read /proc/self/mountinfo\n" );
221
- exit ( 1 );
234
+ errExit ( "fopen" );
222
235
}
223
236
224
237
// try to find line with mount id
0 commit comments