-
Notifications
You must be signed in to change notification settings - Fork 15
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
Try fixing exceptions stuff accordingly to comments in #30 and #36 #52
base: main
Are you sure you want to change the base?
Conversation
include/ifc/abstract-sgraph.hxx
Outdated
static constexpr unsigned NAME_BUFFER_SIZE = 64; | ||
|
||
char name[NAME_BUFFER_SIZE] = ""; |
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.
This could be a std::array<char, 64>
.
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.
ok
include/ifc/abstract-sgraph.hxx
Outdated
{ | ||
InvalidPartitionName e{}; | ||
name = name.substr(0, NAME_BUFFER_SIZE - 1); | ||
strncpy_s(e.name, name.data(), name.length()); |
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.
strncpy_s
is a C11 function, takes 4 arguments and it's not guaranteed to be available: https://en.cppreference.com/w/c/string/byte/strncpy
As with all bounds-checked functions, strncpy_s only guaranteed to be available if
__STDC_LIB_EXT1__
is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__
to the integer constant1
before including <string.h>.
The return value is also not checked.
However, it's very silly to use this function here. This should just be a std::copy
or std::memcpy
.
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.
ok (by the way, in msvc-provided strncpy_s, the function is also a template, in order to avoid passing the size of the destination array manually, but I guess, this is an non-standardized extension on top of an already-optional extension :-))
include/ifc/abstract-sgraph.hxx
Outdated
|
||
char name[NAME_BUFFER_SIZE] = ""; | ||
|
||
static InvalidPartitionName make(std::string_view name) noexcept |
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 a factory function instead of an explicit ctor? Copying from one buffer into another can't fail, so there is no reason to make this a factory function.
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.
In one of my previous attempts (#30 (comment)), @cdacamar told me not to introduce a ctor in this class (but maybe his the rationale does not apply if we have some real work to do anyway in the ctor)
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.
The comment was on an implicit converting ctor that did the same thing the compiler generated one would have, so that comment was valid. However, here the work is non-trivial, so this can be a ctor.
…buffer private to enforce the invariant of always ending with 0)
{ | ||
partition_name = partition_name.substr(0, partition_name_buffer.size() - 1); | ||
std::copy(partition_name.begin(), partition_name.end(), partition_name_buffer.begin()); | ||
partition_name_buffer[partition_name.length()] = 0; |
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.
partition_name
is already sized in such a way that the last byte in the buffer stays the way it was initiliazed, which is 0
. This line is unnecessary.
include/ifc/abstract-sgraph.hxx
Outdated
@@ -3544,7 +3546,20 @@ namespace ifc { | |||
|
|||
// -- exception type in case of an invalid partition name | |||
struct InvalidPartitionName { | |||
std::string_view name; | |||
InvalidPartitionName(std::string_view partition_name) |
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.
Personal nit, but I think converting ctors should be explicit
almost always.
Hi |
Indeed, I think the fixes here are worth evaluating. I have been talking with @GabrielDosReis and we plan on reviewing the changes here sometime later this week or early next week--apologies for the delays. Gaby is unsure about the static buffer change in the exception object, but dynamic memory via something like |
Try a minimal fix:
struct InvalidPartitionName
, replace memberstd::string_view name
bychar[NAME_BUFFER_SIZE] name
and use a class-staticmake
function to initialize the bufferReader::Reader
, replaceif (not ifc.header()) throw "file not found";
byassert(ifc.hader());
translate_exception()
, catch the exceptions of typeInvalidPartitionName
andstd::exception