Compare commits

...

2 Commits

Author SHA1 Message Date
80161b37f7 tape: Respect limits when dynamically encoding
Still need: dynamic decoding, static decoding, static encoding
2025-08-28 09:31:33 -04:00
9d40b81e00 tape: Add limits to the API 2025-08-28 09:31:14 -04:00
3 changed files with 37 additions and 1 deletions

View File

@ -46,9 +46,16 @@ func EncodeAny(encoder *Encoder, value any, tag Tag) (n int, err error) {
case reflect.Uint32: return encoder.WriteUint32(uint32(reflectValue.Uint()))
case reflect.Int64: return encoder.WriteInt64(int64(reflectValue.Int()))
case reflect.Uint64: return encoder.WriteUint64(uint64(reflectValue.Uint()))
case reflect.String: return EncodeAny(encoder, []byte(reflectValue.String()), tag)
case reflect.String:
if reflectValue.Len() > MaxStructureLength {
return 0, ErrTooLong
}
return EncodeAny(encoder, []byte(reflectValue.String()), tag)
}
if reflectValue.CanConvert(reflect.TypeOf(dummyBuffer)) {
if reflectValue.Len() > MaxStructureLength {
return 0, ErrTooLong
}
if tag.Is(LBA) {
nn, err := encoder.WriteUintN(uint64(reflectValue.Len()), tag.CN() + 1)
n += nn; if err != nil { return n, err }
@ -64,8 +71,13 @@ func EncodeAny(encoder *Encoder, value any, tag Tag) (n int, err error) {
case reflect.Slice:
return encodeAnySlice(encoder, value, tag)
// case reflect.Array:
// TODO: we can encode arrays. but can we decode into them?
// that's the fucken question. maybe we just do the first
// return encodeAnySlice(encoder, reflect.ValueOf(value).Slice(0, reflectType.Len()).Interface(), tag)
case reflect.Map:
if reflectValue.Len() > MaxStructureLength {
return 0, ErrTooLong
}
if reflectType.Key() == reflect.TypeOf(uint16(0)) {
return encodeAnyMap(encoder, value, tag)
}

11
tape/error.go Normal file
View File

@ -0,0 +1,11 @@
package tape
// Error enumerates common errors in this package.
type Error string; const (
ErrTooLong Error = "data structure too long"
)
// Error implements the error interface.
func (err Error) Error() string {
return string(err)
}

13
tape/limits.go Normal file
View File

@ -0,0 +1,13 @@
package tape
// MaxStructureLength determines how long a TAPE data structure can be. This
// applies to:
//
// - OTA
// - SBA/LBA
// - KTV
//
// By default it is set at 2^20 (about a million).
// You shouldn't need to change this. If you do, it should only be set once at
// the start of the program.
var MaxStructureLength = 1024 * 1024