Repository: secdev/scapy

Issue:
UnboundLocalError: local variable 'r' referenced before assignment
The local variable r used in sendrecv.py file, line 132 was deleted a few lines above, in 124 line.
This causes:
UnboundLocalError: local variable 'r' referenced before assignment

Scapy version: a683a782ac14e00d0fef40c21ca59eeb149ca3bf

Code fragment of sendrecv.py file:
```python
            del r                          # deleted here
            if notans == 0 and not multi:
                break
            if not ok:
                if verbose > 1:
                    os.write(1, b".")
                nbrecv += 1
                if conf.debug_match:
                    debug.recv.append(r)   # reference here, but already deleted
```


Discussion:
Thanks for the report. Could you provide an example that will help us reproduce the issue?

```python
>>> conf.debug_match = True
>>> srp(Ether()/IP(dst="www.google.com")/ICMP())
Begin emission:
.Finished sending 1 packets.
--------------------------------------------------------UnboundLocalError      Traceback (most recent call last)<ipython-input-6-495eca25c379> in <module>()
----> 1 srp(Ether()/IP(dst="www.google.com")/ICMP())

/home/gpotter/github/scapy/scapy/sendrecv.pyc in srp(x, promisc, iface, iface_hint, filter, nofilter, type, *args, **kargs)
    498         iface = conf.route.route(iface_hint)[0]
    499     s = conf.L2socket(promisc=promisc, iface=iface, filter=filter, nofilter=nofilter, type=type)  # noqa: E501
--> 500     result = sndrcv(s, x, *args, **kargs)
    501     s.close()
    502     return result

/home/gpotter/github/scapy/scapy/sendrecv.pyc in sndrcv(pks, pkt, timeout, inter, verbose, chainCC, retry, multi, rcv_pks, store_unanswered, process, prebuild)
    210         hsent, newans, nbrecv, notans = _sndrcv_rcv(
    211             (rcv_pks or pks), hsent, stopevent, nbrecv, notans, verbose, chainCC, multi,  # noqa: E501
--> 212             _storage_policy=_storage_policy,
    213         )
    214         thread.join()

/home/gpotter/github/scapy/scapy/sendrecv.pyc in _sndrcv_rcv(pks, hsent, stopevent, nbrecv, notans, verbose, chainCC, multi, _storage_policy)
    130                 nbrecv += 1
    131                 if conf.debug_match:
--> 132                     debug.recv.append(r)
    133     except KeyboardInterrupt:
    134         if chainCC:

UnboundLocalError: local variable 'r' referenced before assignment
>>> 
```

Resolving patch:
diff --git a/scapy/sendrecv.py b/scapy/sendrecv.py
--- a/scapy/sendrecv.py
+++ b/scapy/sendrecv.py
@@ -121,8 +121,8 @@ def _get_pkt():
                                 notans -= 1
                             sentpkt._answered = 1
                         break
-            del r
             if notans == 0 and not multi:
+                del r
                 break
             if not ok:
                 if verbose > 1:
@@ -130,6 +130,7 @@ def _get_pkt():
                 nbrecv += 1
                 if conf.debug_match:
                     debug.recv.append(r)
+            del r
     except KeyboardInterrupt:
         if chainCC:
             raise


Test patch:
diff --git a/test/regression.uts b/test/regression.uts
--- a/test/regression.uts
+++ b/test/regression.uts
@@ -1369,6 +1369,20 @@ def _test():
 
 retry_test(_test)
 
+= Send & receive with debug_match
+~ netaccess IP ICMP
+def _test():
+    old_debug_match = conf.debug_match
+    conf.debug_match = True
+    old_debug_dissector = conf.debug_dissector
+    conf.debug_dissector = False
+    ans, unans = sr(IP(dst="www.google.fr") / ICMP(), timeout=2)
+    conf.debug_match = old_debug_match
+    conf.debug_dissector = old_debug_dissector
+    assert ans and not unans
+
+retry_test(_test)
+
 = Send & receive with retry
 ~ netaccess IP ICMP
 def _test():