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

daemon: check payload length before cast to struct #3

Merged
merged 2 commits into from
Jan 31, 2017

Conversation

clipka
Copy link
Contributor

@clipka clipka commented Nov 30, 2016

Check if size of control request structure fits into actual payload datasize. In case it does not fit, return with error message.

Signed-off-by: Christoph Lipka <clipka@jp.adit-jv.com>
@gunnarx
Copy link

gunnarx commented Nov 30, 2016

The fix looks good to me, but too much repetition for my taste. I understand there is repetition in the original code, but this just extends it. Every function (except one) seems to do the same checks and very similar error reporting. Refactor?

@clipka
Copy link
Contributor Author

clipka commented Dec 6, 2016

What do you think about doing the check inside a define and just call it later to reduce the duplication:

/* checks if received size is big enough for expected data. */
/* checks if received size is big enough for expected data */ #define CHECK_DATA_SIZE(received, required) \ ({ \ int _ret = (int)received - (int)required; \ if (_ret < 0) { \ dlt_vlog(LOG_WARNING, "%s: Received data not complete\n", __func__); \ _ret = DLT_RETURN_ERROR; \ } \ else { \ _ret = DLT_RETURN_OK; \ } \ _ret; \ })
call it like this;
if (CHECK_DATA_SIZE(msg->datasize, sizeof(DltServiceGetLogInfoRequest)) < 0) { return; }

Signed-off-by: Christoph Lipka <clipka@jp.adit-jv.com>

Improve macro after review

Signed-off-by: Christoph Lipka <clipka@jp.adit-jv.com>
@gunnarx
Copy link

gunnarx commented Dec 16, 2016

Sorry for late response.

Yes I was thinking something like that exactly, but:

  • Personally I'm usually OK with macros in C if they reduce repetition and complexity. But an inline function might be a type safe alternative, and more accepted by other complainers who feel macros are hacks.
    UPDATE: I see your use of _func_ might need to use a macro to be correct.

  • You rewrote your own check which is good** but looking at the whole I'm thinking we might want to reduce the repetition of the previous checks also. So rather than a macro named "check data size" I would try to extend the helper function/macro to the bigger question we are actually trying to answer : "is input (in)valid?".

**Rather than complain about your bugfix :-) good practice is to do one thing at a time so I'd still propose we take your bugfix in one commit, and then refactor in the next commit.

Do you need the bugfix urgently? In that case we should recommend mering your fix in first.

Let me take a deeper look. Maybe I'll make a proposal based on top of your bugfix if you don't mind - taking the opportunity to do some programming for a change :-)

@clipka
Copy link
Contributor Author

clipka commented Dec 21, 2016

Hi,
ok, then we should take the commit. But I cannot approve my own request :-/

@clipka clipka requested review from GeniviDLTmaintainer and removed request for GeniviDLTmaintainer December 21, 2016 01:51
@gunnarx
Copy link

gunnarx commented Jan 25, 2017

@gunnarx
Copy link

gunnarx commented Jan 25, 2017

Well here's a +1 from me at least, but @GeniviDLTmaintainer should weigh in I guess (or is that you nowadays ;-)

+1 because I figure we could refactor and align error management after this merge

@clipka clipka merged commit 754cee0 into COVESA:master Jan 31, 2017
ssugiura added a commit to ssugiura/dlt-daemon that referenced this pull request Mar 29, 2019
Signed-off-by: Saya Sugiura <ssugiura@jp.adit-jv.com>
ssugiura pushed a commit that referenced this pull request Jul 6, 2020
now you can do make test after make :)
=== Sample build and test:
$ cmake -Bbuild -H. \
          -DDLT_IPC=UNIX_SOCKET \
          -DWITH_DLT_ADAPTOR=ON \
          -DWITH_DLT_UNIT_TESTS=ON \
          -DWITH_DLT_CXX11_EXT=ON \
          -DWITH_DLT_MONITOR=OFF \
          -DWITH_DLT_USE_IPv6=OFF
$ cd build
$ make
$ make test
Running tests...
Test project /home/fherrmann/git/dlt-daemon/build
    Start 1: gtest_dlt_common
1/5 Test #1: gtest_dlt_common .................   Passed    0.02 sec
    Start 2: gtest_dlt_user
2/5 Test #2: gtest_dlt_user ...................   Passed    1.01 sec
    Start 3: gtest_dlt_daemon_common
3/5 Test #3: gtest_dlt_daemon_common ..........   Passed    0.01 sec
    Start 4: dlt_env_ll_unit_test
4/5 Test #4: dlt_env_ll_unit_test .............   Passed    0.04 sec
    Start 5: gtest_dlt_daemon_event_handler
5/5 Test #5: gtest_dlt_daemon_event_handler ...   Passed    1.01 sec

100% tests passed, 0 tests failed out of 5

Total Test time (real) =   2.09 sec
===

Signed-off-by: Felix Herrmann <fherrmann@de.adit-jv.com>
Signed-off-by: KHANH LUONG HONG DUY <khanh.luonghongduy@vn.bosch.com>
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