Skip to content

[Dart] runtime crashes on malformed reference metadata instead of rejecting it cleanly #3627

@miantalha45

Description

@miantalha45

Search before asking

  • I had searched in the issues and found no similar issues.

Version

0.17.0

Component(s)

Other

Minimal reproduce step

Just run this test script for reproducing the issue:

import 'package:fory/src/config.dart';
import 'package:fory/src/context/meta_string_reader.dart';
import 'package:fory/src/context/ref_reader.dart';
import 'package:fory/src/context/ref_writer.dart';
import 'package:fory/src/memory/buffer.dart';
import 'package:fory/src/resolver/type_resolver.dart';
import 'package:test/test.dart';

void main() {
  test('RefReader.readRefOrNull does not throw for an out-of-range ref id',
      () {
    final reader = RefReader();
    final buffer = Buffer();

    buffer.writeByte(RefWriter.refFlag);
    buffer.writeVarUint32(9999);
    bufferSetReaderIndex(buffer, 0);

    expect(
      () => reader.readRefOrNull(buffer),
      returnsNormally,
    );
  });

  test(
      'MetaStringReader.readMetaString does not throw for a negative ref index',
      () {
    final reader = MetaStringReader(TypeResolver(const Config()));
    final buffer = Buffer();

    buffer.writeVarUint32Small7(1);
    bufferSetReaderIndex(buffer, 0);

    expect(
      () => reader.readMetaString(buffer),
      returnsNormally,
    );
  });
}

What did you expect to see?

I expected the Dart runtime to reject the malformed reference data cleanly, without throwing a RangeError or crashing.
For the ref test, I expected readRefOrNull to handle an out-of-range ref id safely.
For the meta-string test, I expected readMetaString to handle the crafted reference header safely instead of indexing -1.

What did you see instead?

The current Dart runtime throws RangeError for both cases.
RefReader.readRefOrNull indexes _refs[id] directly when the ref flag is set, so a large ref id like 9999 fails with an out-of-range error.
MetaStringReader.readMetaString computes length - 1 for a reference header, so header 1 becomes index -1 and fails with a RangeError.

Anything Else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions