glcpp: Handle '#version 0' and other invalid values

The #version directive can only handle decimal constants.  Enforce that
the value is a decimal constant.

Section 3.3 (Preprocessor) of the GLSL 4.50 spec says:

    The language version a shader is written to is specified by

        #version number profile opt

    where number must be a version of the language, following the same
    convention as __VERSION__ above.

The same section also says:

    __VERSION__ will substitute a decimal integer reflecting the version
    number of the OpenGL shading language.

Use a separate flag to track whether or not the #version line has been
encountered.  Any possible sentinel (0 is currently used) could be
specified in a #version directive.  This would lead to trying to
(internally) redefine __VERSION__.  Since there is no parser location
for this addition, NULL is passed.  This eventually results in a NULL
dereference and a segfault.

Attempts to use -1 as the sentinel would also fail if '#version
4294967295' or '#version 18446744073709551615' were used.  We should
have piglit tests for both of these.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=97420
Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
Cc: mesa-stable@lists.freedesktop.org
Cc: Juan A. Suarez Romero <jasuarez@igalia.com>
Cc: Karol Herbst <karolherbst@gmail.com>
This commit is contained in:
Ian Romanick
2016-11-08 11:06:05 -08:00
parent cbba5e13ac
commit e85a747e29
2 changed files with 28 additions and 6 deletions

View File

@@ -177,7 +177,7 @@ add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
* (such as the <HASH> and <DEFINE> start conditions in the lexer). */ * (such as the <HASH> and <DEFINE> start conditions in the lexer). */
%token DEFINED ELIF_EXPANDED HASH_TOKEN DEFINE_TOKEN FUNC_IDENTIFIER OBJ_IDENTIFIER ELIF ELSE ENDIF ERROR_TOKEN IF IFDEF IFNDEF LINE PRAGMA UNDEF VERSION_TOKEN GARBAGE IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE PLUS_PLUS MINUS_MINUS %token DEFINED ELIF_EXPANDED HASH_TOKEN DEFINE_TOKEN FUNC_IDENTIFIER OBJ_IDENTIFIER ELIF ELSE ENDIF ERROR_TOKEN IF IFDEF IFNDEF LINE PRAGMA UNDEF VERSION_TOKEN GARBAGE IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE PLUS_PLUS MINUS_MINUS
%token PASTE %token PASTE
%type <ival> INTEGER operator SPACE integer_constant %type <ival> INTEGER operator SPACE integer_constant version_constant
%type <expression_value> expression %type <expression_value> expression
%type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER ERROR_TOKEN PRAGMA %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER ERROR_TOKEN PRAGMA
%type <string_list> identifier_list %type <string_list> identifier_list
@@ -419,14 +419,14 @@ control_line_success:
| HASH_TOKEN ENDIF { | HASH_TOKEN ENDIF {
_glcpp_parser_skip_stack_pop (parser, & @1); _glcpp_parser_skip_stack_pop (parser, & @1);
} NEWLINE } NEWLINE
| HASH_TOKEN VERSION_TOKEN integer_constant NEWLINE { | HASH_TOKEN VERSION_TOKEN version_constant NEWLINE {
if (parser->version != 0) { if (parser->version_set) {
glcpp_error(& @1, parser, "#version must appear on the first line"); glcpp_error(& @1, parser, "#version must appear on the first line");
} }
_glcpp_parser_handle_version_declaration(parser, $3, NULL, true); _glcpp_parser_handle_version_declaration(parser, $3, NULL, true);
} }
| HASH_TOKEN VERSION_TOKEN integer_constant IDENTIFIER NEWLINE { | HASH_TOKEN VERSION_TOKEN version_constant IDENTIFIER NEWLINE {
if (parser->version != 0) { if (parser->version_set) {
glcpp_error(& @1, parser, "#version must appear on the first line"); glcpp_error(& @1, parser, "#version must appear on the first line");
} }
_glcpp_parser_handle_version_declaration(parser, $3, $4, true); _glcpp_parser_handle_version_declaration(parser, $3, $4, true);
@@ -465,6 +465,17 @@ integer_constant:
$$ = $1; $$ = $1;
} }
version_constant:
INTEGER_STRING {
/* Both octal and hexadecimal constants begin with 0. */
if ($1[0] == '0' && $1[1] != '\0') {
glcpp_error(&@1, parser, "invalid #version \"%s\" (not a decimal constant)", $1);
$$ = 0;
} else {
$$ = strtoll($1, NULL, 10);
}
}
expression: expression:
integer_constant { integer_constant {
$$.value = $1; $$.value = $1;
@@ -1361,6 +1372,7 @@ glcpp_parser_create(glcpp_extension_iterator extensions, void *state, gl_api api
parser->state = state; parser->state = state;
parser->api = api; parser->api = api;
parser->version = 0; parser->version = 0;
parser->version_set = false;
parser->has_new_line_number = 0; parser->has_new_line_number = 0;
parser->new_line_number = 1; parser->new_line_number = 1;
@@ -2293,10 +2305,11 @@ _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t versio
const char *es_identifier, const char *es_identifier,
bool explicitly_set) bool explicitly_set)
{ {
if (parser->version != 0) if (parser->version_set)
return; return;
parser->version = version; parser->version = version;
parser->version_set = true;
add_builtin_define (parser, "__VERSION__", version); add_builtin_define (parser, "__VERSION__", version);

View File

@@ -208,6 +208,15 @@ struct glcpp_parser {
void *state; void *state;
gl_api api; gl_api api;
unsigned version; unsigned version;
/**
* Has the #version been set?
*
* A separate flag is used because any possible sentinel value in
* \c ::version could also be set by a #version line.
*/
bool version_set;
bool has_new_line_number; bool has_new_line_number;
int new_line_number; int new_line_number;
bool has_new_source_number; bool has_new_source_number;