SNMP: patch for Counter32 decode function

Aleksey Morarash <>
Wed Jan 12 15:06:14 CET 2011

Hi all!

I have encountered problems receiving SNMP replies from
ethernet switch HP ProCurve 1400 8G which contain varbinds
of type Counter32. Investigation showed following:
SNMP agent encodes Counter32 as unsigned 32-bit integer,
but snmp_pdus module decodes it as signed integer, as it
described in X.690. So, for some values decode crashes
with error like {error, {bad_counter32, -16276764}}.

Here is example of SNMP reply which snmp_pdus unable to decode:

13:42:12.441281 00:1d:b3:cb:81:20 > 00:1f:d0:8b:ae:fb, ethertype IPv4
(0x0800), length 91: >
GetResponse(34)  .
        0x0000:  4500 004d 0c9a 0000 4011 da1a c0a8 0932  
        0x0010:  c0a8 0969 00a1 9e63 0039 0000 302f 0201  ...i...c.9..0/..
        0x0020:  0104 0670 7562 6c69 63a2 2202 0443 f34f  ...public."..C.O
        0x0030:  3802 0100 0201 0030 1430 1206 0a2b 0601  8......0.0...+..
        0x0040:  0201 0202 010a 6741 04ad 5a7f 54         ......gA..Z.T

Counter32 value 2908389204 is encoded as "ad 5a7f 54".
By the way, tcpdump recognizes value correctly in this case.

I`ve noticed the difference between snmp_pdus in R12B5
and R14B1: in R14B1 additional check added for such buggy
SNMP agent behaviour but only for Counter64 values.

My patch will add such additional check for Counter32
values too.

Also I noticed of a tens "magic numbers" over all snmp_pdus
module which have not neither defined or commented.
I wrote patch in style corresponds common module style,
but looks like somebody must do anything with all of this magic.

Here is the patch:

--- lib/snmp/src/misc/snmp_pdus.erl
+++ lib/snmp/src/misc/snmp_pdus.erl
@@ -272,6 +272,8 @@
     {Value, Rest} = dec_integer_notag(Bytes),
     if Value >= 0, Value =< 4294967295 ->
            {{'Counter32', Value}, Rest};
+       Value >= -2147483648, Value < 0 ->
+           {{'Counter32', 4294967296 + Value}, Rest};
        true ->
            exit({error, {bad_counter32, Value}})

Aleksey Morarash,
e-mail: aleksey.morarash at

More information about the erlang-patches mailing list