Skip to content

Commit cfff1f5

Browse files
committed
Use sizeof() for strncpy's dest, instead of a dissociated constant
This small DRY change also fixes a GCC 8 warning: csv.h:78:48: warning: ‘char* strncpy(char*, const char*, size_t)’ output may be truncated copying 255 bytes from a string of length 255 [-Wstringop-truncation] GCC is unable to determine that error::max_file_name_length is the file_name - using sizeof() helps here.
1 parent 5404611 commit cfff1f5

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

csv.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ namespace io{
7070

7171
struct with_file_name{
7272
with_file_name(){
73-
std::memset(file_name, 0, max_file_name_length+1);
73+
std::memset(file_name, 0, sizeof(file_name));
7474
}
7575

7676
void set_file_name(const char*file_name){
7777
if(file_name != nullptr){
78-
strncpy(this->file_name, file_name, error::max_file_name_length);
79-
this->file_name[error::max_file_name_length] = '\0';
78+
strncpy(this->file_name, file_name, sizeof(this->file_name));
79+
this->file_name[sizeof(this->file_name)-1] = '\0';
8080
}else{
8181
this->file_name[0] = '\0';
8282
}
@@ -422,8 +422,8 @@ namespace io{
422422

423423
void set_file_name(const char*file_name){
424424
if(file_name != nullptr){
425-
strncpy(this->file_name, file_name, error::max_file_name_length);
426-
this->file_name[error::max_file_name_length] = '\0';
425+
strncpy(this->file_name, file_name, sizeof(this->file_name));
426+
this->file_name[sizeof(this->file_name)-1] = '\0';
427427
}else{
428428
this->file_name[0] = '\0';
429429
}

0 commit comments

Comments
 (0)