Thanks for the suggestion.
I'll leave some detail here for posterity.
In case a single goroutine takes care of the decoding the problem can be
solved with a io.LimitedReader. Just (re)set the max number of bytes before
each call to Decode. This approach is nice because it is very trivial, it
is less nice because it breaks with concurrent goroutines and because the
resetting of the byte count is not encapsulated inside the ReadWriter
interface.
The approach Rob suggested solves the concurrency and encapsulation
problems nicely, but requires a (very limited) understanding of gob's
format.
From gob documentation:
*Finally, each message created by a call to Encode is preceded by an
encoded unsigned integer count of the number of bytes remaining in the
message. After the initial type name, interface values are wrapped the same
way; in effect, the interface value acts like a recursive invocation of
Encode.*
How an unsigned integer is encoded:
*An unsigned integer is sent one of two ways. If it is less than 128, it is
sent as a byte with that value. Otherwise it is sent as a minimal-length
big-endian (high byte first) byte stream holding the value, preceded by one
byte holding the byte count, negated. Thus 0 is transmitted as (00), 7 is
transmitted as (07) and 256 is transmitted as (FE 01 00).*
Function to decode an uint:
https://golang.org/src/encoding/gob/decode.go?s=2829:2850#L116On Thursday, October 1, 2015 at 6:20:46 AM UTC+2, Rob 'Commander' Pike
wrote:
It's trivial to break the stream into messages without parsing their
contents, since each message starts with a byte count. It would be very
easy to wrap the I/O going to gob and shut down any connection that sends a
message with a count > N for some N. That is, it would be easy to do this
outside of the gob package. Yay interfaces.
You don't want to limit just strings, so this is a better answer anyway.
The package already does this internally, but the size it accepts is quite
large.
-rob
On Wed, Sep 30, 2015 at 8:28 PM, Roberto Zanotto <roby...@gmail.com
<javascript:>> wrote:
The thing is that I would like to limit the size of a single request, not
the size of the whole stream/connection. But thanks for the answers to both
of you, I understand that the problem can not be addressed by gob as it is
now. Maybe I can accomplish what I want with a customized LimitedReader.
Do you think it might be worth adding to gob the ability to recognize
some struct field tags to limit the size of the data to be decoded/encoded?
It seems like it could be useful for various data types (slices, bigints,
strings...).
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-nuts...@googlegroups.com <javascript:>.
For more options, visit
https://groups.google.com/d/optout.--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.