usb: gadget: f_hid: fix error handling in ghid_setup()
There were a two issues here. 1) We returned PTR_ERR(NULL) which means success if class_create() failed. 2) If alloc_chrdev_region() failed then we should clean up before returning. Also kernel style is to have "error handling" as opposed to "success handling". In the original code checking for "if (!status) " is confusing and this bad style is what lead to bug #2. Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com> Signed-off-by: Felipe Balbi <balbi@ti.com>
This commit is contained in:
		
					parent
					
						
							
								828f6148e8
							
						
					
				
			
			
				commit
				
					
						0448d38c1e
					
				
			
		
					 1 changed files with 10 additions and 5 deletions
				
			
		| 
						 | 
					@ -972,17 +972,22 @@ int ghid_setup(struct usb_gadget *g, int count)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hidg_class = class_create(THIS_MODULE, "hidg");
 | 
						hidg_class = class_create(THIS_MODULE, "hidg");
 | 
				
			||||||
	if (IS_ERR(hidg_class)) {
 | 
						if (IS_ERR(hidg_class)) {
 | 
				
			||||||
 | 
							status = PTR_ERR(hidg_class);
 | 
				
			||||||
		hidg_class = NULL;
 | 
							hidg_class = NULL;
 | 
				
			||||||
		return PTR_ERR(hidg_class);
 | 
							return status;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	status = alloc_chrdev_region(&dev, 0, count, "hidg");
 | 
						status = alloc_chrdev_region(&dev, 0, count, "hidg");
 | 
				
			||||||
	if (!status) {
 | 
						if (status) {
 | 
				
			||||||
		major = MAJOR(dev);
 | 
							class_destroy(hidg_class);
 | 
				
			||||||
		minors = count;
 | 
							hidg_class = NULL;
 | 
				
			||||||
 | 
							return status;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return status;
 | 
						major = MAJOR(dev);
 | 
				
			||||||
 | 
						minors = count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void ghid_cleanup(void)
 | 
					void ghid_cleanup(void)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue