]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - lib/snmplib/snmp_msg.c
Source Format Enforcement (#763)
[thirdparty/squid.git] / lib / snmplib / snmp_msg.c
index 985ad43b015cd6999a8b379d8ff62e79c5366f81..6845a7547c5e2aa93bdd1e4d33928c531941a7db 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
 /*
  * SNMP Message Encoding Routines
  *
@@ -35,8 +43,6 @@
 
 #include "squid.h"
 
-#include <stdio.h>
-
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -188,7 +194,7 @@ snmp_msg_Encode(u_char * Buffer, int *BufLenP,
     PDUDataStart = bufp;
     bufp = snmp_pdu_encode(bufp, BufLenP, PDU);
     if (bufp == NULL)
-        return (NULL);         /* snmp_pdu_encode registered failure */
+        return (NULL);      /* snmp_pdu_encode registered failure */
 
     VARHeaderPtr = bufp;
     bufp = asn_build_header(bufp, BufLenP,
@@ -201,7 +207,7 @@ snmp_msg_Encode(u_char * Buffer, int *BufLenP,
     /* And build the variables */
     bufp = snmp_var_EncodeVarBind(bufp, BufLenP, PDU->variables, Version);
     if (bufp == NULL)
-        return (NULL);         /* snmp_var_EncodeVarBind registered failure */
+        return (NULL);      /* snmp_var_EncodeVarBind registered failure */
 
     /* Cool.  Now insert the appropriate lengths.
      */
@@ -225,14 +231,14 @@ snmp_msg_Encode(u_char * Buffer, int *BufLenP,
     tmp = asn_build_header(Buffer,
                            &FakeArg,
                            (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
-                           (bufp - MsgPtr));   /* Length of everything */
+                           (bufp - MsgPtr));    /* Length of everything */
     if (tmp == NULL)
         return (NULL);
 
     tmp = asn_build_header(VARHeaderPtr,
                            &FakeArg,
                            (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
-                           (bufp - VARDataStart));     /* Length of everything */
+                           (bufp - VARDataStart));  /* Length of everything */
     if (tmp == NULL)
         return (NULL);
 
@@ -266,12 +272,16 @@ snmp_msg_Decode(u_char * Packet, int *PacketLenP,
         snmplib_debug(4, "snmp_msg_Decode:Error decoding SNMP Message Header (Version)!\n");
         ASN_PARSE_ERROR(NULL);
     }
+    int terminatorPos = *CommLenP - 1;
     bufp = asn_parse_string(bufp, PacketLenP, &type, Community, CommLenP);
     if (bufp == NULL) {
         snmplib_debug(4, "snmp_msg_Decode:Error decoding SNMP Message Header (Community)!\n");
         ASN_PARSE_ERROR(NULL);
     }
-    Community[*CommLenP] = '\0';
+    if (*CommLenP < terminatorPos) {
+        terminatorPos = *CommLenP;
+    }
+    Community[terminatorPos] = '\0';
 
     if ((*Version != SNMP_VERSION_1) &&
             (*Version != SNMP_VERSION_2)) {
@@ -295,3 +305,4 @@ snmp_msg_Decode(u_char * Packet, int *PacketLenP,
 
     return (u_char *) bufp;
 }
+