Skip to content

Commit

Permalink
Replaced various sprintf with snprintf
Browse files Browse the repository at this point in the history
snprintf is safer because the buffer length is specified, so can't be overrun.
  • Loading branch information
seanm authored and tbeu committed Feb 7, 2024
1 parent f5f9530 commit 5becf99
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/mat73.c
Original file line number Diff line number Diff line change
Expand Up @@ -1401,11 +1401,11 @@ Mat_VarWriteRef(hid_t id, matvar_t *matvar, enum matio_compression compression,
err = MATIO_E_BAD_ARGUMENT;
} else {
char obj_name[64];
sprintf(obj_name, "%llu", (unsigned long long)group_info.nlinks);
snprintf(obj_name, sizeof(obj_name), "%llu", (unsigned long long)group_info.nlinks);
if ( NULL != matvar )
matvar->compression = compression;
err = Mat_VarWriteNext73(*refs_id, matvar, obj_name, refs_id);
sprintf(obj_name, "/#refs#/%llu", (unsigned long long)group_info.nlinks);
snprintf(obj_name, sizeof(obj_name), "/#refs#/%llu", (unsigned long long)group_info.nlinks);
H5Rcreate(ref, id, obj_name, H5R_OBJECT, -1);
}
return err;
Expand Down
8 changes: 6 additions & 2 deletions tools/matdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,12 @@ print_whos(matvar_t *matvar)
int cnt = 0;
printf("%8" SIZE_T_FMTSTR, matvar->dims[0]);
for ( i = 1; i < matvar->rank; i++ ) {
if ( ceil(log10((double)matvar->dims[i])) + 1 < 32 )
cnt += sprintf(size + cnt, "x%" SIZE_T_FMTSTR, matvar->dims[i]);
if ( ceil(log10((double)matvar->dims[i])) + 1 < 32 ) {
cnt += snprintf(size + cnt, sizeof(size) - cnt, "x%" SIZE_T_FMTSTR, matvar->dims[i]);
if (cnt >= sizeof(size)) {
break;
}
}
}
printf("%-10s", size);
} else {
Expand Down

0 comments on commit 5becf99

Please sign in to comment.