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

[WIP] fix:navit:navigation.c:distance_set_last #373

Merged
merged 8 commits into from Jan 19, 2018

Conversation

charlescurley
Copy link
Contributor

Added a test so that the array subscript is never less than the lower bound. Note: there is no test for the upper bound.

Also, I saw no reason to test for i == 0 right after the code initialized it to zero.

modified:   navit/navigation.c

Added a test so that the array subscript is never less than the lower bound. Note: there is no test for the upper bound.

Also, I saw no reason to test for i == 0 right after the code initialized it to zero.

	modified:   navit/navigation.c
static int
distance_set_last(void)
{
static int i=0;
if (i == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

OK, having time to have a look on what's done in navigation.c I still request the following changes:

  1. Mark the distances array const. Used this way it must never ever change.
  2. Replace distance_set_last function entirely by replacing it by preprocessor math to get the number of items in distances array. Or at least make it return that. Sizeof may be our friend here, to avoid miscounting.
    If messing with some "cute" code as this, better fix it right than replace it by even "cuter" code.

@charlescurley
Copy link
Contributor Author

charlescurley commented Nov 20, 2017 via email

@pgrandin
Copy link
Contributor

pgrandin commented Dec 5, 2017

@metalstrolch would you like to update your review following @charlescurley 's comment?

@metalstrolch
Copy link
Contributor

OK, having time to have a look on what's done in navigation.c I still request the following changes:

  1. Mark the distances array const. Used this way it must never ever change.
  2. Replace distance_set_last function entirely by replacing it by preprocessor math to get the number of items in distances array. Or at least make it return that. Sizeof may be our friend here, to avoid miscounting.

If messing with some "cute" code as this, better fix it right than replace it by even "cuter" code.

entirely and use compile-time maths to do the work.

fix:navit:navigation.c

	modified:   navit/navigation.c
@charlescurley
Copy link
Contributor Author

charlescurley commented Dec 6, 2017 via email

@metalstrolch
Copy link
Contributor

metalstrolch commented Dec 6, 2017

This way it looks good. The way the number of elements is defined now seems to be quite common.

I wouldn't care too much about real exotic archs.
But you're right about the PDP11 and maybe the Honeywells of the same time.
Modern DSPs. that sometimes have char with 16 or 32 bit, are not affected by a miscalculation here. It's really only the PDP11.
Btw: shouldn't the PDP11 have 16 bit chars nowadays? Nothing says a char must be equal to 8 bit wide. It just says at least 8 bit as far as I remember.

@charlescurley
Copy link
Contributor Author

charlescurley commented Dec 6, 2017 via email

in an array (#define SIZE_OF_ARRAY_DISTANCES).

comment:navit:navigation.c

	modified:   navit/navigation.c
@pgrandin
Copy link
Contributor

I want to run through this code with gdb just to be sure everything is on the level, and haven't figured out yet how to get it called. So let's not call it good yet.

I've renamed your PR as a WIP for now, please remove when you feel that it's ready for review.

@pgrandin pgrandin changed the title fix:navit:navigation.c:distance_set_last [WIP] fix:navit:navigation.c:distance_set_last Dec 13, 2017
@sleske
Copy link
Contributor

sleske commented Dec 15, 2017

@charlescurley:
Kudos for thinking thoroughly about the compatibility of your C code.
Just a standard-nitpicking note:

The PDP 11 has a word size of 16 bits. Chars are 8 bits, but align on 16 bit boundaries. An array of chars has one char in each word of memory. So doing this would fail:

char array[] = {1,2,3};
#define SIZE_OF_ARRAY (sizeof (array)/sizeof (char))

While I can't speak for the PDP 11, this cannot happen in standard C (as defined by the ISO/IEC standards, i.e. C99 etc). In standard C, an array is a "contiguously allocated nonempty set of objects", so no padding is allowed between elements. This means that in order for a type to be used in an array, its alignment must divide its size. See for example Can C arrays contain padding in between elements? on stackoverflow.com for details.

tl;dr: We don't need to worry about this, the C standard has our back 📕.

@pgrandin
Copy link
Contributor

pgrandin commented Jan 9, 2018

so @charlescurley good to go? your PR still says "WIP"

@charlescurley
Copy link
Contributor Author

charlescurley commented Jan 9, 2018 via email

@pgrandin
Copy link
Contributor

Does anyone else think it necessary? Given sleske's discussion, I am not convinced it is necessary.

Yep, I would agree.

@pgrandin pgrandin merged commit 852c5e3 into trunk Jan 19, 2018
@pgrandin pgrandin deleted the distance_set_last.return branch January 19, 2018 21:51
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.

None yet

4 participants