Skip to content

protoc: parsing of float/double numeric values for options is inconsistent #15010

@jhump

Description

@jhump

The parser has a special grammar for parsing default values that allows infinity, -infinity, and NaN. However, this grammar is not used when parsing other numeric option values.

Take the following example:

syntax = "proto2";

import "google/protobuf/descriptor.proto";

extend google.protobuf.MessageOptions {
  optional Foo foo = 10101;
}

message Foo {
  optional double val = 1 [default = -nan]; // "nan", "-nan", "inf", and "-inf" allowed here
  repeated float vals = 2;

  option (foo) = { val: -infINITY }; // aside: inside a message literal, anything goes 😭

  option (foo).vals = inf; // "nan", "-nan", "inf", and "-inf" NOT allowed here (??)
  // The above line results in the following error from protoc:
  //    test.proto:15:23: Value must be number for float option "Foo.vals".
  // If the value were -inf (negative), the message changes:
  //    test.proto:15:24: Invalid '-' symbol before identifier.
}

I assert that it's a bug that infinity and NaN can only be used in default values and not other option values. It seems like a simple overlooked thing that is easy to fix.

(As shown above, the contents of message literals, which are parsed using the text format, are very lenient. I'm certainly not suggesting that anything else in protoc should be that forgiving -- just pointing it out in the example since it's... interesting.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementinactiveDenotes the issue/PR has not seen activity in the last 90 days.protoc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions