Skip to content

Commit

Permalink
fix compiler warnings about potentially truncated strings from snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
axxel authored and msmeissn committed Sep 16, 2024
1 parent 33b4f05 commit aa4197e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
24 changes: 8 additions & 16 deletions camlibs/canon/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -2419,19 +2419,15 @@ make_dir_func (CameraFilesystem __unused__ *fs, const char *folder,
if (strlen (folder) > 1) {
/* folder is something more than / */

if (strlen (folder) + 1 + strlen (name) > sizeof (gppath) - 1) {
GP_DEBUG ("make_dir_func: Arguments too long");
if (snprintf (gppath, sizeof(gppath), "%s/%s", folder, name) >= (int)sizeof(gppath)) {
GP_LOG_E ("Arguments too long");
return GP_ERROR_BAD_PARAMETERS;
}

snprintf (gppath, sizeof(gppath), "%s/%s", folder, name);
} else {
if (1 + strlen (name) > sizeof (gppath) - 1) {
GP_DEBUG ("make_dir_func: Arguments too long");
if (snprintf (gppath, sizeof(gppath), "/%s", name) >= (int)sizeof(gppath)) {
GP_LOG_E ("Arguments too long");
return GP_ERROR_BAD_PARAMETERS;
}

snprintf (gppath, sizeof(gppath), "/%s", name);
}

canonpath = gphoto2canonpath (camera, gppath, context);
Expand Down Expand Up @@ -2461,19 +2457,15 @@ remove_dir_func (CameraFilesystem __unused__ *fs, const char *folder,
if (strlen (folder) > 1) {
/* folder is something more than / */

if (strlen (folder) + 1 + strlen (name) > sizeof (gppath) - 1) {
GP_DEBUG ("make_dir_func: Arguments too long");
if (snprintf (gppath, sizeof(gppath), "%s/%s", folder, name) >= (int)sizeof(gppath)) {
GP_LOG_E ("Arguments too long");
return GP_ERROR_BAD_PARAMETERS;
}

snprintf (gppath, sizeof(gppath), "%s/%s", folder, name);
} else {
if (1 + strlen (name) > sizeof (gppath) - 1) {
GP_DEBUG ("make_dir_func: Arguments too long");
if (snprintf (gppath, sizeof(gppath), "/%s", name) >= (int)sizeof(gppath)) {
GP_LOG_E ("Arguments too long");
return GP_ERROR_BAD_PARAMETERS;
}

snprintf (gppath, sizeof(gppath), "/%s", name);
}

canonpath = gphoto2canonpath (camera, gppath, context);
Expand Down
3 changes: 2 additions & 1 deletion camlibs/ptp2/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -5653,7 +5653,8 @@ camera_sigma_fp_capture (Camera *camera, CameraCaptureType type, CameraFilePath

C_PTP_REP (ptp_sigma_fp_clearimagedbsingle(params, captstatus.imageid));

snprintf (path->name, sizeof(path->name), "%s%s", pictfileinfoex2.name, pictfileinfoex2.fileext);
if (snprintf (path->name, sizeof(path->name), "%s%s", pictfileinfoex2.name, pictfileinfoex2.fileext) >= (int)sizeof(path->name))
GP_LOG_E("pictfileinfoex2.name and .fileext did not fit into path->name");
strcpy (path->folder,"/");

ret = gp_file_new (&file);
Expand Down
3 changes: 2 additions & 1 deletion camlibs/st2205/st2205.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ enum {
all unique and don't change when files with the same name (after converting
to ascii and truncating) are added / deleted. */
#define ST2205_SET_FILENAME(dest, name, idx) \
snprintf(dest, sizeof(st2205_filename), "%04d-%s.png", (idx) + 1, name)
if (snprintf(dest, sizeof(st2205_filename), "%04d-%s.png", (idx) + 1, name) >= (int)sizeof(st2205_filename)) \
GP_LOG_E ("extended st2205_filename did not fit into dest")

struct st2205_coord {
uint16_t x;
Expand Down
3 changes: 2 additions & 1 deletion libgphoto2_port/usbdiskdirect/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ gp_port_usbdiskdirect_resolve_symlink (const char *link)
} else {
*slash = 0;
len = strlen (path);
snprintf (path + len, sizeof (path) - len, "/%s", buf);
if (snprintf (path + len, sizeof (path) - len, "/%s", buf) >= (int)sizeof (path) - len)
return NULL;
}

if (stat (path, &st))
Expand Down
3 changes: 2 additions & 1 deletion libgphoto2_port/usbscsi/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ gp_port_usbscsi_resolve_symlink (const char *link)
} else {
*slash = 0;
len = strlen (path);
snprintf (path + len, sizeof (path) - len, "/%s", buf);
if (snprintf (path + len, sizeof (path) - len, "/%s", buf) >= (int)sizeof (path) - len)
return NULL;
}

if (stat (path, &st))
Expand Down

0 comments on commit aa4197e

Please sign in to comment.