google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot build with -Wconversion -Werror [C++, gcc >= 9, Flatbuffers 2.0.6]

avaliente-bc opened this issue · comments

I'm trying to build a C++ project using flatbuffers 2.0.6. This project uses -Wconversion and -Werror.
When I'm including my generated C++ file, that have #include "flatbuffers/flatbuffers.h", I get the following errors:

In file included from /build/src/src/FlatBuffers/include/flatbuffers/verifier.h:21,
                 from /build/src/src/FlatBuffers/include/flatbuffers/buffer_ref.h:21,
                 from /build/src/src/FlatBuffers/include/flatbuffers/flatbuffers.h:25,
                 from /build/src/engine/inc/engine/akida_device_program_fb_generated.h:7,
                 from /src/engine/program_play.cpp:10:
/build/src/src/FlatBuffers/include/flatbuffers/util.h: In function ‘bool flatbuffers::is_alpha(char)’:
/build/src/src/FlatBuffers/include/flatbuffers/util.h:52:30: error: conversion from ‘int’ to ‘char’ may change value [-Werror=conversion]
   52 |   return check_ascii_range(c & 0xDF, 'a' & 0xDF, 'z' & 0xDF);
      |                            ~~^~~~~~
/build/src/src/FlatBuffers/include/flatbuffers/util.h: In function ‘bool flatbuffers::is_xdigit(char)’:
/build/src/src/FlatBuffers/include/flatbuffers/util.h:74:45: error: conversion from ‘int’ to ‘char’ may change value [-Werror=conversion]
   74 |   return is_digit(c) || check_ascii_range(c & 0xDF, 'a' & 0xDF, 'f' & 0xDF);
      |                                           ~~^~~~~~

This is a weird behaviour because it looks like c & 0xDF is treated as an int, while 'a' & 0xDF is treated as a char.
This is reproduced with GCC 9.4.0 and 11.2.0.
An easy fix would be to static_cast<char>(c & 0xDF)

I just submitted 1461569 which should make it easy to add this warning. Would you like to do a PR for this and fix those issues?

Actually there's a lot more changes to do than I first saw with my local build to allow -Wconversion to pass

ezoic increase your site revenue