Copyright ©

Mongoose OS Forum

ATTENTION! This forum has moved to:

Do not post any new messages.

Setting _FILE_OFFSET_BITS in mongoose.h can cause hard to find errors with mg_stat

I am using mongoose 6.13 and I encountered a situation that can caused some what the ... moments.

Imagine the following situation on a Linux (possibly other as well) platform.

#include <system includes>
#include "mongoose.h"
cs_stat_t st;
mg_stat(filename, &st);

My code acted up with what seemed to be a stack corruption. And indeed it was.
- the system includes defined struct stat
- as the _FILE_OFFSET_BITS was not defined a system default was used, which in my case was 32-bit
- the mongoose.h typedef'ed it to cs_stat_t
- my code passed a struct stat with 32 bit file offsets as the second mg_stat parameter

In mongoose.c however something other happened. mongoose.h contains

#define _FILE_OFFSET_BITS 64

and includes the system stuff after this. The mg_stat thus used statand struct stat expecting 64-bit file offsets and clobbered 8 more bytes at the st pointer.

Moving the mongoose.h before the rest fixed the problem.

Frankly I have no idea whether this is a bug or not. To me it looks like forcing the define is a bad idea and it would be better to use the system default unless the user did explicitely set it but maybe there is some good reason to do that. This post should at least help someone encountering this.


  • SergeySergey Dublin, Ireland
    edited December 2018

    That's intentional!
    Thanks for the write-up though.

    Thanked by 1numo68
Sign In or Register to comment.