-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Compile with -Werror and -Wall #1116
Changes from 8 commits
1b65490
a9fac12
f36f5de
d91c08b
ddc9c2f
8d51c79
573ec8b
804aeac
7801c73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -369,7 +369,7 @@ int64_t read_vector(int fd, int64_t *type, std::vector<uint8_t> &buffer) { | |
if (closed) { | ||
goto disconnected; | ||
} | ||
if (length > buffer.size()) { | ||
if (static_cast<size_t>(length) > buffer.size()) { | ||
buffer.resize(length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would "work" but it's very confusing and wouldn't make sense if length is negative. There should at least be a cast here. But see my comment above. |
||
} | ||
closed = read_bytes(fd, buffer.data(), length); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,10 +72,10 @@ class TaskBuilder { | |
} | ||
|
||
void SetRequiredResource(int64_t resource_index, double value) { | ||
if (resource_index >= resource_vector_.size()) { | ||
if (static_cast<size_t>(resource_index) >= resource_vector_.size()) { | ||
/* Make sure the resource vector is constructed entry by entry, | ||
* in order. */ | ||
CHECK(resource_index == resource_vector_.size()); | ||
CHECK(static_cast<size_t>(resource_index) == resource_vector_.size()); | ||
resource_vector_.resize(resource_index + 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is especially confusing... are you adding 1 to a potentially negative value? Is that what you intend? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you saying that in general casting from signed to unsigned is bad? E.g., would it be better to do the following? CHECK(resource_index == static_cast<int64_t>(resource_vector_.size())); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I'm saying (like in the previous comment) the variable should be unsigned. Right now you're adding 1 to a signed value and then resizing based on that, which doesn't make sense in the negative case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So in our case we have decided to make all index variables signed and we try to keep it that way; this is also done by others like Google and Arrow so we are in good company. The reason is that it simplifies arithmetic and guards against the variable wrapping around zero (it is easier to detect a negative index than a large positive one). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Signed values don't actually simplify anything. (?) They only make it complicated to range-check, since now you have to check both Moreover, what there is a difference in is that overflowing signed values is in fact undefined rather than well-defined behavior. So that means your code could do all sorts of crazy things if you in fact do happen to accidentally overflow, rather than merely writing to a lower spot than the one you intended to. More generally, I would not hold Google as the example of ideal C++ coding. They have guidelines that are pretty universally understood to be often bad advice (no need to take my word for this; just Google around...), and tailored to their own particular use cases. I don't know why Arrow might do this but I'm sure you won't have trouble finding people who use unsigned values either... =P That said, you obviously don't have to change this if you'd rather not. Just letting you know what the issues are. |
||
} | ||
resource_vector_[resource_index] = value; | ||
|
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.
Why not just have
length
beuint64_t
(I'm assumingsize_t
is not an option)?It's better to let the data type match the semantics of the variable than to check for it. Otherwise if you pass around signed variables for unsigned quantities then you'll keep having to worry about whether a negative value is possible and if it might have a special meaning.