Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in truncate function #48

Closed
agudallago opened this issue Sep 23, 2015 · 9 comments
Closed

Bug in truncate function #48

agudallago opened this issue Sep 23, 2015 · 9 comments

Comments

@agudallago
Copy link

Hello,

I was trying to truncate a file using the function spiffs_object_truncate() and I noticed it was not working properly with smaller files. (I tried to truncate a 51 bytes file to 5, for example).

So I noticed this line inside its implementation:

if (cur_size - SPIFFS_DATA_PAGE_SIZE(fs) >= new_size)

In this case, both cur_size and new_size are u32_t. For this reason, even though the left argument in the comparison should be negative, it is not, it is a a very big unsigned int and therefore this condition becomes true when it should be false.

So I changed this line to the following and the issue seemed to be fixed.

if ((s32_t)cur_size - (s32_t)(SPIFFS_DATA_PAGE_SIZE(fs)) >= (s32_t)new_size)

Please confirm that this spotted issue is correct!

Thanks

@yihcdaso-yeskela
Copy link

I think, we can stay unsigned

if (cur_size >= new_size + SPIFFS_DATA_PAGE_SIZE(fs))

Not tested!!!!

@pellepl
Copy link
Owner

pellepl commented Sep 23, 2015

Hmm yes, you're right. My bad, an attempt to get rid of warnings, a bit to
hasty it seems. I'll try it out. Thanks!
Den 23 sep 2015 16:18 skrev "Aleksey Osadchiy" notifications@github.com:

Think, we can stay unsigned

if (cur_size >= new_size + SPIFFS_DATA_PAGE_SIZE(fs))

Not tested!!!!


Reply to this email directly or view it on GitHub
#48 (comment).

@pellepl
Copy link
Owner

pellepl commented Sep 23, 2015

Well, that was a bit trickier than I thought. Some tests fail after modification - I need to dig a bit deeper in this. Again, thanks for finding this!
I guess it has never been a problem as the API only exports remove (i.e. truncate to zero). I'll roll up the sleeves and get dirty.

@agudallago
Copy link
Author

As I said. I think that my fix works pretty good. Am I mistaken?
On 23 Sep 2015 6:57 pm, "Peter Andersson" notifications@github.com wrote:

Well, that was a bit trickier than I thought. Some tests fail after
modification - I need to dig a bit deeper in this. Again, thanks for
finding this!
I guess it has never been a problem as the API only exports remove (i.e.
truncate to zero). I'll roll up the sleeves and get dirty.


Reply to this email directly or view it on GitHub
#48 (comment).

@pellepl
Copy link
Owner

pellepl commented Sep 23, 2015

After I applied the fix (any of them) the regression tests fail - it seems something messes up the LUTs. Which is weird, because the fix should be ok. That's why I have to investigate further.

@pellepl
Copy link
Owner

pellepl commented Sep 23, 2015

Update: it seems the fix works when truncating but messes up when fully removing files. Digging on.

pellepl added a commit that referenced this issue Sep 23, 2015
Unsigned overflow bug, and actually missing tests when
truncating to other than zero.
@pellepl
Copy link
Owner

pellepl commented Sep 23, 2015

Ok, fixed. Needed some more massaging than just casting, more idiocies from my side.

@pellepl pellepl closed this as completed Sep 23, 2015
@pellepl
Copy link
Owner

pellepl commented Sep 23, 2015

Oh and thanks yet again ;)

@agudallago
Copy link
Author

It's been a pleasure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants