Code

6cdaa1c6f5ab1a1876920b1c2bad17993e018cfd
[pkg-collectd.git] / debian / patches / bts770688_snmp_memleak.dpatch
1 #! /bin/sh /usr/share/dpatch/dpatch-run
2 ## bts770688_snmp_memleak.dpatch by Pierre-Yves Ritschard <pyr@spootnik.org>
3 ## and Marc Fournier <marc.fournier@camptocamp.com>
4 ##
5 ## DP:  Let snmp_synch_response deal with PDU freeing
6 ## DP: 
7 ## DP: When reading from tables, upon errors the PDUs sent are already
8 ## DP: freed by snmp_synch_response since they are right after
9 ## DP: snmp_send is called.
10 ## DP: 
11 ## DP: This commit syncs collectd's approach with other occurences of
12 ## DP: snmp_synch_response calls.
13 ## DP: 
14 ## DP: There might be a few corner cases where we leak PDUs, but it
15 ## DP: is unclear how to check for those since we would need to
16 ## DP: have an indication that snmp_send was never called, which
17 ## DP: as far as I can tell is not possible.
18 ## DP: 
19 ## DP: The potential for failure in snmp_send is rather low and will
20 ## DP: be easily spotted though, since when crafting invalid PDUs
21 ## DP: snmp send will constantly fail and since valid configurations
22 ## DP: can never leak memory.
23 ## DP:
24 ## DP: Upstream bug reports:
25 ## DP: https://github.com/collectd/collectd/issues/610
26 ## DP: https://github.com/collectd/collectd/issues/804
28 @DPATCH@
30 diff --git a/src/snmp.c b/src/snmp.c
31 index ad81c89..7d340d1 100644
32 --- a/src/snmp.c
33 +++ b/src/snmp.c
34 @@ -1316,6 +1316,8 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
35          snmp_free_pdu (res);
36        res = NULL;
37  
38 +      /* snmp_synch_response already freed our PDU */
39 +      req = NULL;
40        sfree (errstr);
41        csnmp_host_close_session (host);
42  
43 @@ -1437,6 +1439,10 @@ static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
44      snmp_free_pdu (res);
45    res = NULL;
46  
47 +  if (req != NULL)
48 +    snmp_free_pdu (req);
49 +  req = NULL;
50 +
51    if (status == 0)
52      csnmp_dispatch_table (host, data, instance_list_head, value_list_head);
53