forked from mendersoftware/mender-mcu
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: address Preloop review findings for RAM staging #1
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
Closed
ajlennon
wants to merge
1
commit into
feature/zephyr-ram-stage-on-main
from
fix/preloop-review-findings
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * @file image-ram-stage.c | ||
| * @brief Whole-image RAM staging helpers (platform-independent) | ||
| * | ||
| * Copyright Northern.tech AS | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #include "alloc.h" | ||
| #include "image-ram-stage.h" | ||
|
|
||
| mender_err_t | ||
| mender_image_ram_stage_begin(mender_image_ram_stage_t *stage, size_t size, bool enabled, size_t max_size) { | ||
| if (NULL == stage) { | ||
| return MENDER_FAIL; | ||
| } | ||
|
|
||
| mender_image_ram_stage_reset(stage); | ||
|
|
||
| if (!enabled || 0 == size) { | ||
| return MENDER_OK; | ||
| } | ||
| if ((max_size > 0) && (size > max_size)) { | ||
| return MENDER_OK; | ||
| } | ||
|
|
||
| stage->buf = mender_malloc(size); | ||
| if (NULL == stage->buf) { | ||
| return MENDER_OK; | ||
| } | ||
| stage->capacity = size; | ||
| stage->length = 0; | ||
| return MENDER_OK; | ||
| } | ||
|
|
||
| bool | ||
| mender_image_ram_stage_active(const mender_image_ram_stage_t *stage) { | ||
| return (NULL != stage) && (NULL != stage->buf); | ||
| } | ||
|
|
||
| mender_err_t | ||
| mender_image_ram_stage_write(mender_image_ram_stage_t *stage, const void *data, size_t index, size_t length) { | ||
| if ((NULL == stage) || (NULL == stage->buf) || (NULL == data)) { | ||
| return MENDER_FAIL; | ||
| } | ||
| if (index + length > stage->capacity) { | ||
| return MENDER_FAIL; | ||
| } | ||
| memcpy(stage->buf + index, data, length); | ||
| if (index + length > stage->length) { | ||
| stage->length = index + length; | ||
| } | ||
| return MENDER_OK; | ||
| } | ||
|
|
||
| void | ||
| mender_image_ram_stage_reset(mender_image_ram_stage_t *stage) { | ||
| if (NULL == stage) { | ||
| return; | ||
| } | ||
| if (NULL != stage->buf) { | ||
| mender_free(stage->buf); | ||
| } | ||
| stage->buf = NULL; | ||
| stage->capacity = 0; | ||
| stage->length = 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /** | ||
| * @file image-ram-stage.h | ||
| * @brief Whole-image RAM staging helpers (platform-independent) | ||
| * | ||
| * Copyright Northern.tech AS | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef __MENDER_IMAGE_RAM_STAGE_PRIV_H__ | ||
| #define __MENDER_IMAGE_RAM_STAGE_PRIV_H__ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif /* __cplusplus */ | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stddef.h> | ||
| #include <stdint.h> | ||
|
|
||
| #include <mender/utils.h> | ||
|
|
||
| typedef struct mender_image_ram_stage { | ||
| uint8_t *buf; | ||
| size_t capacity; | ||
| size_t length; | ||
| } mender_image_ram_stage_t; | ||
|
|
||
| /** | ||
| * @brief Try to allocate a staging buffer for @p size bytes | ||
| * @param stage Staging state (must not be NULL) | ||
| * @param size Artifact payload size | ||
| * @param enabled When false, leave stage inactive (direct-to-flash) | ||
| * @param max_size When > 0 and size > max_size, leave stage inactive | ||
| * @return MENDER_OK always (inactive stage is a valid outcome); MENDER_FAIL on bad args | ||
| * | ||
| * Allocation uses mender_malloc. Failure to allocate leaves the stage inactive. | ||
| */ | ||
| mender_err_t mender_image_ram_stage_begin(mender_image_ram_stage_t *stage, size_t size, bool enabled, size_t max_size); | ||
|
|
||
| bool mender_image_ram_stage_active(const mender_image_ram_stage_t *stage); | ||
|
|
||
| /** | ||
| * @brief Copy a download chunk into the staging buffer | ||
| * @return MENDER_OK, or MENDER_FAIL on overflow / inactive stage / bad args | ||
| */ | ||
| mender_err_t mender_image_ram_stage_write(mender_image_ram_stage_t *stage, const void *data, size_t index, size_t length); | ||
|
|
||
| /** Free staging buffer and clear state. */ | ||
| void mender_image_ram_stage_reset(mender_image_ram_stage_t *stage); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif /* __cplusplus */ | ||
|
|
||
| #endif /* __MENDER_IMAGE_RAM_STAGE_PRIV_H__ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[LOW] [Quality]
Affected files:
src/core/image-ram-stage.c:31-46mender_image_ram_stage_beginunconditionally callsmender_image_ram_stage_reset(stage), whichmender_freesstage->buf. If a caller ever passes a non-zero-initialized struct (e.g. a stackmender_image_ram_stage_t stage;without= {0}),resetcould free a garbage pointer. The only current caller is a zero-initializedstatic, and the unit tests use= {}, so this is latent rather than active.Recommendation:
Document in the header that the struct must be zero-initialized before
begin, or only free whenbuf != NULLafter an internal init guard.⏳ Still open as of commit
0da7502— this issue has not been addressed yet.