{"action":"opened","issue":{"url":"https://api.github.com/repos/tegesoft/flash-stm32f407/issues/2","repository_url":"https://api.github.com/repos/tegesoft/flash-stm32f407","labels_url":"https://api.github.com/repos/tegesoft/flash-stm32f407/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/tegesoft/flash-stm32f407/issues/2/comments","events_url":"https://api.github.com/repos/tegesoft/flash-stm32f407/issues/2/events","html_url":"https://github.com/tegesoft/flash-stm32f407/issues/2","id":137067621,"number":2,"title":"flashErase() bug","user":{"login":"DrTon","id":4341525,"avatar_url":"https://avatars.githubusercontent.com/u/4341525?v=3","gravatar_id":"","url":"https://api.github.com/users/DrTon","html_url":"https://github.com/DrTon","followers_url":"https://api.github.com/users/DrTon/followers","following_url":"https://api.github.com/users/DrTon/following{/other_user}","gists_url":"https://api.github.com/users/DrTon/gists{/gist_id}","starred_url":"https://api.github.com/users/DrTon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DrTon/subscriptions","organizations_url":"https://api.github.com/users/DrTon/orgs","repos_url":"https://api.github.com/users/DrTon/repos","events_url":"https://api.github.com/users/DrTon/events{/privacy}","received_events_url":"https://api.github.com/users/DrTon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-02-28T16:12:22Z","updated_at":"2016-02-28T16:12:22Z","closed_at":null,"body":"Thanks for the nice library, but I have just found a bug:\r\n - `size_t` is unsigned type, so `size` variable is always >= 0.\r\n - check `while (size > 0)` actually means `while (size != 0)`\r\n - if `size` given in argument != size of sector this will never give 0: `size -= flashSectorSize(sector)`\r\n\r\nSorry, no time to make PR, fixed code, use if you like it:\r\n\r\n```\r\nint flashErase(flashaddr_t address, size_t size)\r\n{\r\n    while (size > 0)\r\n    {\r\n        flashsector_t sector = flashSectorAt(address);\r\n        int err = flashSectorErase(sector);\r\n        if (err != FLASH_RETURN_SUCCESS)\r\n            return err;\r\n        address = flashSectorEnd(sector);\r\n        size_t sector_size = flashSectorSize(sector);\r\n        if (sector_size >= size) {\r\n            break;\r\n        } else {\r\n            size -= sector_size;\r\n        }\r\n    }\r\n\r\n    return FLASH_RETURN_SUCCESS;\r\n}\r\n```\r\n\r\nI haven't checked other places yet, probably the same bug there."}}