From: Martin Ling Date: Tue, 31 Mar 2015 23:24:27 +0000 (+0100) Subject: Clarify sp_list_ports() code. X-Git-Tag: libserialport-0.1.1~74 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=8d12e620f255ef44da8d0c7ee810a7b3bcaf8807;p=libserialport.git Clarify sp_list_ports() code. This also fixes the following scan-build warning: serialport.c:335:15: warning: Result of 'malloc' is converted to a pointer of type 'struct sp_port *', which is incompatible with sizeof operand type 'struct sp_port **' if (!(list = malloc(sizeof(struct sp_port **)))) ^~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ --- diff --git a/serialport.c b/serialport.c index f5859e2..b8ddffe 100644 --- a/serialport.c +++ b/serialport.c @@ -330,31 +330,27 @@ SP_API enum sp_return sp_list_ports(struct sp_port ***list_ptr) if (!list_ptr) RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); +#ifdef NO_ENUMERATION + RETURN_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform"); +#else DEBUG("Enumerating ports"); - if (!(list = malloc(sizeof(struct sp_port **)))) + if (!(list = malloc(sizeof(struct sp_port *)))) RETURN_ERROR(SP_ERR_MEM, "Port list malloc failed"); list[0] = NULL; -#ifdef NO_ENUMERATION - ret = SP_ERR_SUPP; -#else ret = list_ports(&list); -#endif - switch (ret) { - case SP_OK: + if (ret == SP_OK) { *list_ptr = list; - RETURN_OK(); - case SP_ERR_SUPP: - DEBUG_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform"); - default: - if (list) - sp_free_port_list(list); + } else { + sp_free_port_list(list); *list_ptr = NULL; - return ret; } + + RETURN_CODEVAL(ret); +#endif } SP_API void sp_free_port_list(struct sp_port **list)