/* HEADER oss-fuzz bug 40420 */
/*
 * Verify whether agentx_parse() does not trigger a memory leak for a
 * particular invalid AgentX input. See also
 * https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=40420.
 */

netsnmp_session session;
netsnmp_pdu *pdu;
int rc;
static u_char data[] = {
    0x57, 0x04, 0x00, 0x43, 0x43, 0x43, 0x43, 0x48,
    0x43, 0x3d, 0xe4, 0x44, 0x43, 0x41, 0xff, 0xff,
    0x41, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x31, 0xc8, 0x80, 0xda, 0xaf, 0x00, 0x82, 0x01,
    0x00, 0x03, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf,
    0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0xdf, 0x06,
    0xc0, 0x04, 0xff, 0x0a, 0x0a, 0x0a, 0x0a, 0xdf,
    0xfb, 0x42, 0x02, 0x01, 0xff, 0x30, 0x0c, 0x30,
    0xfe, 0xff, 0xff, 0xff, 0x06, 0x5d, 0xff, 0xff,
    0x56, 0xff, 0x2a, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
    0x4d, 0x6b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0x3f, 0xff, 0xff, 0xff, 0xf7, 0x31, 0x00, 0x00,
    0x28, 0x02, 0xff, 0x0a, 0x44, 0x2f, 0x38, 0x36,
    0x30, 0x2f, 0x02, 0x01, 0x01, 0x40, 0x02, 0x02,
    0x02, 0xa7, 0x26, 0x02, 0x02, 0x02, 0x42, 0x02,
    0x06, 0x25, 0xfa, 0xff, 0xdf, 0xfb, 0x42, 0x02,
    0x01, 0xff, 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x01,
    0x7a, 0x06, 0x05, 0xff, 0xa1, 0x02, 0x02, 0x02,
    0x42, 0x02, 0x06, 0xc0, 0x04, 0xff, 0x0a, 0x0a,
    0x0a, 0x0a, 0xdf, 0xfb, 0x42, 0x02, 0x01, 0xff,
    0x30, 0x0c, 0x30, 0xfe, 0xff, 0xff, 0xff, 0x06,
    0x5d, 0xff, 0xff, 0x56, 0xff, 0x2a, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xfe, 0x4d, 0x6b, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xfe, 0x4d, 0x6b, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xf7,
    0x31, 0x31, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf7,
    0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0x42,
    0x87, 0x02, 0x01, 0xff, 0x30, 0x0c, 0x31, 0x0a,
    0x06, 0x01, 0x7a, 0x06, 0x05, 0xff, 0xff, 0x56,
    0xff, 0x2a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x4d,
    0x6b, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0x3f,
    0x09, 0x00, 0x00, 0x08, 0xce, 0xce, 0x00, 0x00,
    0xfe, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
    0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x01, 0xff, 0x42, 0x87, 0x08, 0x06, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x52, 0x7a, 0x7a, 0x7a
};
static const size_t data_length = sizeof(data);

memset(&session, 0, sizeof(session));
session.version = AGENTX_VERSION_1;

pdu = calloc(1, sizeof(*pdu));

rc = agentx_parse(&session, pdu, data, data_length);
snmp_free_pdu(pdu);

fprintf(stderr, "rc = %d\n", rc);
fflush(stderr);

OKF(rc == 0, ("Parsing of AgentX data succeeded"));

netsnmp_cleanup_session(&session);
