Source file src/encoding/gob/decoder.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gob
     6  
     7  import (
     8  	"bufio"
     9  	"errors"
    10  	"io"
    11  	"reflect"
    12  	"sync"
    13  )
    14  
    15  // tooBig provides a sanity check for sizes; used in several places. Upper limit
    16  // of is 1GB on 32-bit systems, 8GB on 64-bit, allowing room to grow a little
    17  // without overflow.
    18  const tooBig = (1 << 30) << (^uint(0) >> 62)
    19  
    20  // A Decoder manages the receipt of type and data information read from the
    21  // remote side of a connection.  It is safe for concurrent use by multiple
    22  // goroutines.
    23  //
    24  // The Decoder does only basic sanity checking on decoded input sizes,
    25  // and its limits are not configurable. Take caution when decoding gob data
    26  // from untrusted sources.
    27  type Decoder struct {
    28  	mutex        sync.Mutex                              // each item must be received atomically
    29  	r            io.Reader                               // source of the data
    30  	buf          decBuffer                               // buffer for more efficient i/o from r
    31  	wireType     map[typeId]*wireType                    // map from remote ID to local description
    32  	decoderCache map[reflect.Type]map[typeId]**decEngine // cache of compiled engines
    33  	ignorerCache map[typeId]**decEngine                  // ditto for ignored objects
    34  	freeList     *decoderState                           // list of free decoderStates; avoids reallocation
    35  	countBuf     []byte                                  // used for decoding integers while parsing messages
    36  	err          error
    37  }
    38  
    39  // NewDecoder returns a new decoder that reads from the io.Reader.
    40  // If r does not also implement io.ByteReader, it will be wrapped in a
    41  // bufio.Reader.
    42  func NewDecoder(r io.Reader) *Decoder {
    43  	dec := new(Decoder)
    44  	// We use the ability to read bytes as a plausible surrogate for buffering.
    45  	if _, ok := r.(io.ByteReader); !ok {
    46  		r = bufio.NewReader(r)
    47  	}
    48  	dec.r = r
    49  	dec.wireType = make(map[typeId]*wireType)
    50  	dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
    51  	dec.ignorerCache = make(map[typeId]**decEngine)
    52  	dec.countBuf = make([]byte, 9) // counts may be uint64s (unlikely!), require 9 bytes
    53  
    54  	return dec
    55  }
    56  
    57  // recvType loads the definition of a type.
    58  func (dec *Decoder) recvType(id typeId) {
    59  	// Have we already seen this type? That's an error
    60  	if id < firstUserId || dec.wireType[id] != nil {
    61  		dec.err = errors.New("gob: duplicate type received")
    62  		return
    63  	}
    64  
    65  	// Type:
    66  	wire := new(wireType)
    67  	dec.decodeValue(tWireType, reflect.ValueOf(wire))
    68  	if dec.err != nil {
    69  		return
    70  	}
    71  	// Remember we've seen this type.
    72  	dec.wireType[id] = wire
    73  }
    74  
    75  var errBadCount = errors.New("invalid message length")
    76  
    77  // recvMessage reads the next count-delimited item from the input. It is the converse
    78  // of Encoder.writeMessage. It returns false on EOF or other error reading the message.
    79  func (dec *Decoder) recvMessage() bool {
    80  	// Read a count.
    81  	nbytes, _, err := decodeUintReader(dec.r, dec.countBuf)
    82  	if err != nil {
    83  		dec.err = err
    84  		return false
    85  	}
    86  	if nbytes >= tooBig {
    87  		dec.err = errBadCount
    88  		return false
    89  	}
    90  	dec.readMessage(int(nbytes))
    91  	return dec.err == nil
    92  }
    93  
    94  // readMessage reads the next nbytes bytes from the input.
    95  func (dec *Decoder) readMessage(nbytes int) {
    96  	if dec.buf.Len() != 0 {
    97  		// The buffer should always be empty now.
    98  		panic("non-empty decoder buffer")
    99  	}
   100  	// Read the data
   101  	dec.buf.Size(nbytes)
   102  	_, dec.err = io.ReadFull(dec.r, dec.buf.Bytes())
   103  	if dec.err == io.EOF {
   104  		dec.err = io.ErrUnexpectedEOF
   105  	}
   106  }
   107  
   108  // toInt turns an encoded uint64 into an int, according to the marshaling rules.
   109  func toInt(x uint64) int64 {
   110  	i := int64(x >> 1)
   111  	if x&1 != 0 {
   112  		i = ^i
   113  	}
   114  	return i
   115  }
   116  
   117  func (dec *Decoder) nextInt() int64 {
   118  	n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
   119  	if err != nil {
   120  		dec.err = err
   121  	}
   122  	return toInt(n)
   123  }
   124  
   125  func (dec *Decoder) nextUint() uint64 {
   126  	n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
   127  	if err != nil {
   128  		dec.err = err
   129  	}
   130  	return n
   131  }
   132  
   133  // decodeTypeSequence parses:
   134  // TypeSequence
   135  //	(TypeDefinition DelimitedTypeDefinition*)?
   136  // and returns the type id of the next value. It returns -1 at
   137  // EOF.  Upon return, the remainder of dec.buf is the value to be
   138  // decoded. If this is an interface value, it can be ignored by
   139  // resetting that buffer.
   140  func (dec *Decoder) decodeTypeSequence(isInterface bool) typeId {
   141  	firstMessage := true
   142  	for dec.err == nil {
   143  		if dec.buf.Len() == 0 {
   144  			if !dec.recvMessage() {
   145  				// We can only return io.EOF if the input was empty.
   146  				// If we read one or more type spec messages,
   147  				// require a data item message to follow.
   148  				// If we hit an EOF before that, then give ErrUnexpectedEOF.
   149  				if !firstMessage && dec.err == io.EOF {
   150  					dec.err = io.ErrUnexpectedEOF
   151  				}
   152  				break
   153  			}
   154  		}
   155  		// Receive a type id.
   156  		id := typeId(dec.nextInt())
   157  		if id >= 0 {
   158  			// Value follows.
   159  			return id
   160  		}
   161  		// Type definition for (-id) follows.
   162  		dec.recvType(-id)
   163  		if dec.err != nil {
   164  			break
   165  		}
   166  		// When decoding an interface, after a type there may be a
   167  		// DelimitedValue still in the buffer. Skip its count.
   168  		// (Alternatively, the buffer is empty and the byte count
   169  		// will be absorbed by recvMessage.)
   170  		if dec.buf.Len() > 0 {
   171  			if !isInterface {
   172  				dec.err = errors.New("extra data in buffer")
   173  				break
   174  			}
   175  			dec.nextUint()
   176  		}
   177  		firstMessage = false
   178  	}
   179  	return -1
   180  }
   181  
   182  // Decode reads the next value from the input stream and stores
   183  // it in the data represented by the empty interface value.
   184  // If e is nil, the value will be discarded. Otherwise,
   185  // the value underlying e must be a pointer to the
   186  // correct type for the next data item received.
   187  // If the input is at EOF, Decode returns io.EOF and
   188  // does not modify e.
   189  func (dec *Decoder) Decode(e any) error {
   190  	if e == nil {
   191  		return dec.DecodeValue(reflect.Value{})
   192  	}
   193  	value := reflect.ValueOf(e)
   194  	// If e represents a value as opposed to a pointer, the answer won't
   195  	// get back to the caller. Make sure it's a pointer.
   196  	if value.Type().Kind() != reflect.Pointer {
   197  		dec.err = errors.New("gob: attempt to decode into a non-pointer")
   198  		return dec.err
   199  	}
   200  	return dec.DecodeValue(value)
   201  }
   202  
   203  // DecodeValue reads the next value from the input stream.
   204  // If v is the zero reflect.Value (v.Kind() == Invalid), DecodeValue discards the value.
   205  // Otherwise, it stores the value into v. In that case, v must represent
   206  // a non-nil pointer to data or be an assignable reflect.Value (v.CanSet())
   207  // If the input is at EOF, DecodeValue returns io.EOF and
   208  // does not modify v.
   209  func (dec *Decoder) DecodeValue(v reflect.Value) error {
   210  	if v.IsValid() {
   211  		if v.Kind() == reflect.Pointer && !v.IsNil() {
   212  			// That's okay, we'll store through the pointer.
   213  		} else if !v.CanSet() {
   214  			return errors.New("gob: DecodeValue of unassignable value")
   215  		}
   216  	}
   217  	// Make sure we're single-threaded through here.
   218  	dec.mutex.Lock()
   219  	defer dec.mutex.Unlock()
   220  
   221  	dec.buf.Reset() // In case data lingers from previous invocation.
   222  	dec.err = nil
   223  	id := dec.decodeTypeSequence(false)
   224  	if dec.err == nil {
   225  		dec.decodeValue(id, v)
   226  	}
   227  	return dec.err
   228  }
   229  
   230  // If debug.go is compiled into the program, debugFunc prints a human-readable
   231  // representation of the gob data read from r by calling that file's Debug function.
   232  // Otherwise it is nil.
   233  var debugFunc func(io.Reader)
   234  

View as plain text