]> granicus.if.org Git - esp-idf/commitdiff
netconn_gethostbyname: Fix race reporting success
authorJan Schmidt <jan@centricular.com>
Fri, 20 Jan 2017 03:17:03 +0000 (14:17 +1100)
committerAngus Gratton <angus@espressif.com>
Fri, 20 Jan 2017 03:57:00 +0000 (14:57 +1100)
If the DNS request is dispatched and performed very quickly,
then it can complete before tcpip_callback() actually returns,
in which case we'll destroy the actual err_t error value passed
in the message. Use a local variable for the tcpip_callback
error code so that can't happen.

Resolves #269 https://github.com/espressif/esp-idf/pull/269

components/lwip/api/api_lib.c

index 087115f0b9df22d541fe02f3df9ef54193b397c8..e5c247f0bc840b38c5232d5e66343c48d499e437 100755 (executable)
@@ -880,10 +880,11 @@ netconn_gethostbyname(const char *name, ip_addr_t *addr)
 {
 
   API_VAR_DECLARE(struct dns_api_msg, msg);
+  err_t localerr;
 #if !LWIP_MPU_COMPATIBLE
   sys_sem_t sem;
 #endif /* LWIP_MPU_COMPATIBLE */
-  err_t err;
+  err_t err = ERR_OK;
 
   LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
   LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
@@ -918,14 +919,14 @@ netconn_gethostbyname(const char *name, ip_addr_t *addr)
   }
 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
 
-  err = tcpip_callback(lwip_netconn_do_gethostbyname, &API_VAR_REF(msg));
-  if (err != ERR_OK) {
+  localerr = tcpip_callback(lwip_netconn_do_gethostbyname, &API_VAR_REF(msg));
+  if (localerr != ERR_OK) {
 #if !LWIP_NETCONN_SEM_PER_THREAD
     sys_sem_free(API_EXPR_REF(API_VAR_REF(msg).sem));
 #endif /* !LWIP_NETCONN_SEM_PER_THREAD */
 
     API_VAR_FREE(MEMP_DNS_API_MSG, msg);
-    return err;
+    return localerr;
   }
 
   sys_sem_wait(API_EXPR_REF_SEM(API_VAR_REF(msg).sem));