Flatbuffers: flatebuffer unmarshal wrong data will panic [golang 1.10,Windows10,1.8.0]

Created on 18 May 2018  Â·  9Comments  Â·  Source: google/flatbuffers

// ByteVector gets a byte slice from data stored inside the flatbuffer.
func (t *Table) ByteVector(off UOffsetT) []byte {
    off += GetUOffsetT(t.Bytes[off:])
    start := off + UOffsetT(SizeUOffsetT)
    length := GetUOffsetT(t.Bytes[off:])
    return t.Bytes[start : start+length]
}

panic: runtime error: slice bounds out of range

how to avoid it panic?

stale2

All 9 comments

Yes, it is intended to panic on invalid data, as it may have offsets that point to outside the buffer. We do not want to do manual error checking on each access for performance and API simplicity.

In C++ we have an additional verifier that can tell if a buffer is safe to read before accessing it (because C++ has no bounds checking at all). This could be ported to Go if that is useful.

@rw who wrote the Go port.

thanks reply, now we plan to use golang's recover to catch panics in each fuction

@GStones did you get your issue resolved? If not, please post your schema file so we can take a look.

one of our schema files:

include "Common.fbs";
namespace msg_fbs;

enum HallMessage:int32 {
    CGEnterHall= 20000,
    GCEnterHall,

    CGMatchBattle,
    GCMatchBattle,

    GCMatchOver,

    CGCancelMatchBattle,
    GCCancelMatchBattle,

    GCHallAddPlayer,
    GCHallRemovePlayer,

    CGLeaveHall,
    GCLeaveHall,

    CGHallPlayerReady,
    GCHallPlayerReady,

    CGHallPlayerCancelReady,
    GCHallPlayerCancelReady,

    CGHallTransferTeamOwner,
    GCHallUpdateTeamOwner, 
}

enum HallErrCode : byte { 
         Success = 0,
         NotLogin, 
         NotExist,
         NotFound, 
         AlreadyIn,
         OverMax,
         LogicErr,
         DataErr,
         NotReady
 }

table HallRole{
    UID:string;
    SeatID:int32;
    Name:string;
    Gender:RoleGender;
    Avatar:int32;
    StarLevel:int32;
    Team:BattleTeam;//delete
    RankStage:int32;            
    RankLevel:int32;            
}
enum BattleLevel:byte{Esay = 0,Normal,Hard}

table CGEnterHall{
    UID:string;//delete
    MatchMode:MatchMode;     
    TargetUID:string;       
}

table GCEnterHall{
    Code:HallErrCode;
}

table CGCancelMatchBattle{}
table GCCancelMatchBattle{
    Code:HallErrCode;   
}

table CGMatchBattle {}   
table GCMatchBattle {
    Code:HallErrCode;   
    Duration:int64;    //second
}

table GCMatchOver{
    RoomID:int64;   
    Roles:[HallRole];// delete
}

//------------Team-------------------
table GCHallAddPlayer{
    Roles:[HallRole];
}

table GCHallRemovePlayer{
    UID:string;
}

table GCHallUpdateTeamOwner{
    UID:string;
}

table CGHallTransferTeamOwner{
    UID:string;
}

table CGLeaveHall{
    TeamID:string;//new
}
table GCLeaveHall{
    Code:HallErrCode;
}

table CGHallPlayerReady{}     
table GCHallPlayerReady{
    UID:string;
}

table CGHallPlayerCancelReady{}
table GCHallPlayerCancelReady{
    UID:string;
}





server must keep stable

when client send error data,the server will panic with _runtime error: slice bounds out of range_
Sorry, I didn’t respond in time.

This issue has been automatically marked as stale because it has not had activity for 1 year. It will be automatically closed if no further activity occurs. To keep it open, simply post a new comment. Maintainers will re-open on new activity. Thank you for your contributions.

don‘t leave me alone,this still not resolve

@GStones what would resolve this? You have 3 possible solutions:

  • Somehow ensure your data is not corrupted before you reach Go code, i.e. by only working with trusted servers, or use external verification mechanisms (C++).
  • Somehow catch the panic.
  • Get someone to port the C++ verifier to Go.

ok! thanks for your reply.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eduardosm picture eduardosm  Â·  3Comments

Dmitry-N-Medvedev picture Dmitry-N-Medvedev  Â·  9Comments

shepmaster picture shepmaster  Â·  8Comments

tymcauley picture tymcauley  Â·  3Comments

siebeneicher picture siebeneicher  Â·  6Comments