Fixed buffer overflow
This commit is contained in:
		@@ -54,24 +54,32 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Construct the full path
 | 
					    // Construct the full path
 | 
				
			||||||
    char fullpath[PATH_MAX];
 | 
					    char *fullpath = NULL;
 | 
				
			||||||
    snprintf(fullpath, PATH_MAX, "%s/%s", path, entry->d_name);
 | 
					    if (asprintf(&fullpath, "%s/%s", path, entry->d_name) == -1 ||
 | 
				
			||||||
 | 
					        fullpath == NULL) {
 | 
				
			||||||
 | 
					      perror("asprintf");
 | 
				
			||||||
 | 
					      success = 0;
 | 
				
			||||||
 | 
					      break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Stat the entry to check if it's a regular file
 | 
					    // Stat the entry to check if it's a regular file
 | 
				
			||||||
    struct stat entry_stat;
 | 
					    struct stat entry_stat;
 | 
				
			||||||
    if (lstat(fullpath, &entry_stat) == -1) {
 | 
					    if (lstat(fullpath, &entry_stat) == -1) {
 | 
				
			||||||
      perror("lstat");
 | 
					      perror("lstat");
 | 
				
			||||||
      success = 0;
 | 
					      success = 0;
 | 
				
			||||||
 | 
					      free(fullpath);
 | 
				
			||||||
      break;
 | 
					      break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Only process regular files
 | 
					    // Only process regular files
 | 
				
			||||||
    if (!S_ISREG(entry_stat.st_mode)) {
 | 
					    if (!S_ISREG(entry_stat.st_mode)) {
 | 
				
			||||||
 | 
					      free(fullpath);
 | 
				
			||||||
      continue;
 | 
					      continue;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Try to open and immediately close the file
 | 
					    // Try to open and immediately close the file
 | 
				
			||||||
    int fd = open(fullpath, O_RDONLY);
 | 
					    int fd = open(fullpath, O_RDONLY);
 | 
				
			||||||
 | 
					    free(fullpath);
 | 
				
			||||||
    if (fd == -1) {
 | 
					    if (fd == -1) {
 | 
				
			||||||
      perror("open");
 | 
					      perror("open");
 | 
				
			||||||
      success = 0;
 | 
					      success = 0;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user