diff --git a/snmp/snmp.c b/snmp/snmp.c index f88d8a0..740b3aa 100644 --- a/snmp/snmp.c +++ b/snmp/snmp.c @@ -58,8 +58,12 @@ char *request(void *sessp, long max_repetitions, oid *name, size_t name_length, if (status != STAT_SUCCESS) { char *errstr = session_error(sessp); char *err = NULL; - asprintf(&err, "SNMP request error: %s", errstr); - free(errstr); + int failure = asprintf(&err, "SNMP request error: %s", errstr); + if (failure == -1) { + err = errstr; + } else { + free(errstr); + } return err; } @@ -119,8 +123,12 @@ struct bulkwalk_response *bulkwalk(char *peername, char *community) { if ((sessp = snmp_sess_open(&session)) == NULL) { char *errstr = open_error(&session); char *err = NULL; - asprintf(&err, "Open SNMP session error: %s", errstr); - free(errstr); + int failure = asprintf(&err, "Open SNMP session error: %s", errstr); + if (failure == -1) { + err = errstr; + } else { + free(errstr); + } add_error(response, err); return response; } @@ -163,8 +171,12 @@ struct bulkwalk_response *bulkwalk(char *peername, char *community) { char *errstr = session_error(sessp); err = NULL; - asprintf(&err, "SNMP response error (%ld): %s", subtree->errstat, errstr); - free(errstr); + int failure = asprintf(&err, "SNMP response error (%ld): %s", subtree->errstat, errstr); + if (failure == -1) { + err = errstr; + } else { + free(errstr); + } add_error(response, err); snmp_free_pdu(subtree); break; diff --git a/snmp/snmp.h b/snmp/snmp.h index aa2fd0c..f9c2c4e 100644 --- a/snmp/snmp.h +++ b/snmp/snmp.h @@ -6,6 +6,13 @@ license that can be found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd */ +// This makes asprintf work properly under GNU. +#ifdef __GNUC__ +# ifndef _GNU_SOURCE +# define _GNU_SOURCE +# endif // _GNU_SOURCE +#endif //__GNUC__ + #include // size_t #include // asprintf #include // calloc, realloc, free