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

Fix src/check_pack.c #250

Merged
merged 5 commits into from
Mar 9, 2020
Merged

Fix src/check_pack.c #250

merged 5 commits into from
Mar 9, 2020

Conversation

mikkoi
Copy link
Contributor

@mikkoi mikkoi commented Mar 1, 2020

Some refactoring done in src/check_pack.c. The interface src/check_pack.h is unchanged. Because of this, I have added several casts. I have tried to work around the problems of signed/unsigned arithmetic.

mikkoi added 4 commits March 7, 2020 13:02
    * Check before calling the function that input value fits
      in 32 bits.

    Attn. This refactoring is done without changing the interface
    (check_pack.h), including the messages. To do this, a lot of
    paranoid programming is done to ensure values fit the types.

Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
    New signatures:
    static size_t pack_ctx(char **buf, CtxMsg * cmsg)
    static size_t pack_loc(char **buf, LocMsg * lmsg)
    static size_t pack_fail(char **buf, FailMsg * fmsg)
    static size_t pack_duration(char **buf, DurationMsg * fmsg)

    * Interface functions (check_pack.h) pack() and upack() return int
      so return values are checked if they fit int type.

    Attn. This refactoring is done without changing the interface
    (check_pack.h), including the messages. To do this, a lot of
    paranoid programming is done to ensure values fit the types.

Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
    * Create macro CK_DIAGNOSTIC_PUSH_IGNORE and CK_DIAGNOSTIC_POP
    * Ignore two cases of -Wbad-function-cast because
      temporary variables would just be completely useless

Signed-off-by: Mikko Johannes Koivunalho <[email protected]>
I wanted to have this also but not possible (explain below):

if(n > SSIZE_MAX)
    eprintf("get_result returned value (%z) too big for ssize_t, max allowed %d\n",

    * Do not use SSIZE_MAX (with ssize_t) because it is POSIX.
      This creates additional and confusing casts, unfortunately.

Signed-off-by: Mikko Johannes Koivunalho <[email protected]>


static void pack_int(char **buf, int val);
static int upack_int(char **buf);
static void pack_int(char **buf, ck_uint32 val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering what the value of having the ck_uint32 typedef is, rather than just using uint32_t directly. I'm not sure it buys anything. Same for UINT32_MAX vs CK_UINT32_MAX. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, ck_uint32 was there when I came. 😁
I added CK_UINT32_MAX for the sake of consistence.
ck_uint32 is only used in check_pack.c and only internally, not in the header. It would not be a big thing to refactor it away.

src/check_pack.c Outdated
@@ -196,7 +202,11 @@ static char *upack_str(char **buf)
char *val;
int strsz;

strsz = upack_int(buf);
ck_uint32 str_sz = upack_int(buf);
if(str_sz > INT_MAX)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for the pack and unpack are not the same. A check for UINT32_MAX is done for the pack, but a check for INT_MAX is done for the unpack. Should the check be the same, and use the smaller of UINT32_MAX or INT_MAX?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the point looking at some of the other commits that do the same thing. It is not about catching an issue when the value is first packed, but doing a sanity check before the value is used. That should be fine. It catches an issue before the data is used improperly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It checks that the value is not too big. In fact, I am unhappy about writing a ck_uint32 to an int, because int's size could be smaller (and system dependent). In fact, if int is 32 bit, it is certainly too small because of the unsigned to signed conversion.
But DurationMsg is part of the API (the public header). If that were changed, it would remove the need for many checks.

@brarcher
Copy link
Contributor

brarcher commented Mar 9, 2020

Note to self: Travis-CI deprecated how it reports status back on builds. A build was triggered and passed: link.

@brarcher brarcher merged commit 55d8ee2 into libcheck:master Mar 9, 2020
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

Successfully merging this pull request may close these issues.

2 participants