I just ran the static analyser cppcheck over the source code of
bibutils-4.15. It said
[modsout.c:300]: (error) Buffer is accessed out of bounds: pos [modsout.c:306]: (error) Buffer is accessed out of bounds: pos [modsout.c:312]: (error) Buffer is accessed out of bounds: pos
Thanks for the report. Actually the code is fine (if misleading) and cppcheck is
confused.
In the function definition, the "3" in the "int pos[3]" isn't actually used by the
compiler (the only indices that are important are those before the last one in
multidimensional arrays so the compiler can do the appropriate math to properly
index the data). In this case, the function definition could also easily use
"int pos[]" as well.
In the calling function, the array passed is properly an integery array of size 4
so there are no problems. (But clearly I forgot to update the "int pos[3]" in the
function definition.)
I will fix this up in 4.16 so that cppcheck will be happy.
Chris.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello there,
I just ran the static analyser cppcheck over the source code of
bibutils-4.15. It said
[modsout.c:300]: (error) Buffer is accessed out of bounds: pos
[modsout.c:306]: (error) Buffer is accessed out of bounds: pos
[modsout.c:312]: (error) Buffer is accessed out of bounds: pos
The source code is
int i, found = -1, ntypes = 4;
for ( i=0; i<ntypes; ++i ) {
pos[i] = fields_find( f, src_names[i], level );
but
output_finddateissued( fields *f, int level, int pos[3] )
Suggest code rework.
Regards
David Binderman
Thanks for the report. Actually the code is fine (if misleading) and cppcheck is
confused.
In the function definition, the "3" in the "int pos[3]" isn't actually used by the
compiler (the only indices that are important are those before the last one in
multidimensional arrays so the compiler can do the appropriate math to properly
index the data). In this case, the function definition could also easily use
"int pos[]" as well.
In the calling function, the array passed is properly an integery array of size 4
so there are no problems. (But clearly I forgot to update the "int pos[3]" in the
function definition.)
I will fix this up in 4.16 so that cppcheck will be happy.
Chris.