Skip to content

Commit

Permalink
add a few missing C_MEM() checks for realloc/strdup (#1006)
Browse files Browse the repository at this point in the history
Co-authored-by: axxel <[email protected]>
  • Loading branch information
axxel and axxel authored Sep 5, 2024
1 parent 742b4db commit a9091de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
12 changes: 6 additions & 6 deletions camlibs/ptp2/chdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ chdk_generic_script_run (
case PTP_CHDK_TYPE_STRING:
GP_LOG_D("string %s", msg->data);
if (*table) {
*table = realloc(*table,strlen(*table)+strlen(msg->data)+1);
C_MEM (*table = realloc(*table,strlen(*table)+strlen(msg->data)+1));
strcat(*table,msg->data);
} else {
*table = strdup(msg->data);
C_MEM (*table = strdup(msg->data));
}
break;
case PTP_CHDK_TYPE_TABLE:
GP_LOG_D("table %s", msg->data);
if (*table) {
*table = realloc(*table,strlen(*table)+strlen(msg->data)+1);
C_MEM (*table = realloc(*table,strlen(*table)+strlen(msg->data)+1));
strcat(*table,msg->data);
} else {
*table = strdup(msg->data);
C_MEM (*table = strdup(msg->data));
}
break;
default: GP_LOG_E("unknown chdk msg->type %d", msg->subtype);break;
Expand Down Expand Up @@ -178,7 +178,7 @@ chdk_list_func (CameraFilesystem *fs, const char *folder, CameraList *list,
char *xfolder;

/* strip leading / of folders, except for the root folder */
xfolder=strdup(folder);
C_MEM (xfolder=strdup(folder));
if (strlen(folder)>2 && (xfolder[strlen(xfolder)-1] == '/'))
xfolder[strlen(xfolder)-1] = '\0';

Expand Down Expand Up @@ -250,7 +250,7 @@ chdk_list_func (CameraFilesystem *fs, const char *folder, CameraList *list,
name = t+strlen("name=.");
s = strchr(name,'"');
if (s) *s='\0';
name = strdup(name);
C_MEM (name = strdup(name));
GP_LOG_D("name is %s", name);
*s = '"';
}
Expand Down
32 changes: 16 additions & 16 deletions camlibs/ptp2/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -12298,27 +12298,27 @@ camera_lookup_by_property(Camera *camera, PTPDevicePropDesc *dpd, char **name, c
continue;
}
CR (gp_widget_get_name (widget, (const char**)name));
*name = strdup(*name?*name:"<null>");
C_MEM (*name = strdup(*name?*name:"<null>"));
CR (gp_widget_get_type (widget, &type));
switch (type) {
case GP_WIDGET_RADIO:
case GP_WIDGET_MENU:
case GP_WIDGET_TEXT: {
char *val;
CR (gp_widget_get_value (widget, &val));
*content = strdup(val?val:"<null>");
C_MEM (*content = strdup(val?val:"<null>"));
break;
}
case GP_WIDGET_RANGE: {
char buf[20];
float fval;
CR (gp_widget_get_value (widget, &fval));
sprintf(buf,"%f",fval);
*content = strdup(buf);
C_MEM (*content = strdup(buf));
break;
}
default:
*content = strdup("Unhandled type");
C_MEM (*content = strdup("Unhandled type"));
/* FIXME: decode value ... ? date? toggle? */
break;
}
Expand All @@ -12338,27 +12338,27 @@ camera_lookup_by_property(Camera *camera, PTPDevicePropDesc *dpd, char **name, c
continue;
}
CR (gp_widget_get_name (widget, (const char**)name));
*name = strdup(*name);
C_MEM (*name = strdup(*name));
CR (gp_widget_get_type (widget, &type));
switch (type) {
case GP_WIDGET_RADIO:
case GP_WIDGET_MENU:
case GP_WIDGET_TEXT: {
char *val;
CR (gp_widget_get_value (widget, &val));
*content = strdup(val);
C_MEM (*content = strdup(val));
break;
}
case GP_WIDGET_RANGE: {
char buf[20];
float fval;
CR (gp_widget_get_value (widget, &fval));
sprintf(buf,"%f",fval);
*content = strdup(buf);
C_MEM (*content = strdup(buf));
break;
}
default:
*content = strdup("Unhandled type");
C_MEM (*content = strdup("Unhandled type"));
/* FIXME: decode value ... ? date? toggle? */
break;
}
Expand All @@ -12376,27 +12376,27 @@ camera_lookup_by_property(Camera *camera, PTPDevicePropDesc *dpd, char **name, c
continue;
}
CR (gp_widget_get_name (widget, (const char**)name));
*name = strdup(*name);
C_MEM (*name = strdup(*name));
CR (gp_widget_get_type (widget, &type));
switch (type) {
case GP_WIDGET_RADIO:
case GP_WIDGET_MENU:
case GP_WIDGET_TEXT: {
char *val;
CR (gp_widget_get_value (widget, &val));
*content = strdup(val?val:"NULL");
C_MEM (*content = strdup(val?val:"NULL"));
break;
}
case GP_WIDGET_RANGE: {
char buf[20];
float fval;
CR (gp_widget_get_value (widget, &fval));
sprintf(buf,"%f",fval);
*content = strdup(buf);
C_MEM (*content = strdup(buf));
break;
}
default:
*content = strdup("Unhandled type");
C_MEM (*content = strdup("Unhandled type"));
/* FIXME: decode value ... ? date? toggle? */
break;
}
Expand All @@ -12417,12 +12417,12 @@ camera_lookup_by_property(Camera *camera, PTPDevicePropDesc *dpd, char **name, c
sprintf (buf, N_("PTP Property 0x%04x"), propid);
label = buf;
}
*name = strdup (label);
C_MEM (*name = strdup (label));
switch (dpd->DataType) {
#define X(dtc,val,fmt) \
case dtc: \
sprintf (buf, fmt, dpd->CurrentValue.val); \
*content = strdup (buf); \
C_MEM (*content = strdup (buf)); \
return GP_OK; \

X(PTP_DTC_INT8,i8,"%d")
Expand All @@ -12435,11 +12435,11 @@ camera_lookup_by_property(Camera *camera, PTPDevicePropDesc *dpd, char **name, c
X(PTP_DTC_UINT64,u64,"%ld")
#undef X
case PTP_DTC_STR:
*content = strdup (dpd->CurrentValue.str?dpd->CurrentValue.str:"<null>");
C_MEM (*content = strdup (dpd->CurrentValue.str?dpd->CurrentValue.str:"<null>"));
return GP_OK;
default:
sprintf(buf, "Unknown type 0x%04x", dpd->DataType);
*content = strdup (buf);
C_MEM (*content = strdup (buf));
return GP_OK;
}
}
Expand Down
9 changes: 3 additions & 6 deletions camlibs/ptp2/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,7 @@ fixup_cached_deviceinfo (Camera *camera, PTPDeviceInfo *di) {
/* The J5 so far goes up to 0xf01c */
#define NIKON_1_ADDITIONAL_DEVPROPS 29
if (i==di->DevicePropertiesSupported_len) {
di->DevicePropertiesSupported = realloc(di->DevicePropertiesSupported,sizeof(di->DevicePropertiesSupported[0])*(di->DevicePropertiesSupported_len + NIKON_1_ADDITIONAL_DEVPROPS+3));
if (!di->DevicePropertiesSupported) {
C_MEM (di->DevicePropertiesSupported);
}
C_MEM (di->DevicePropertiesSupported = realloc(di->DevicePropertiesSupported,sizeof(di->DevicePropertiesSupported[0])*(di->DevicePropertiesSupported_len + NIKON_1_ADDITIONAL_DEVPROPS+3)));
for (i=0;i<NIKON_1_ADDITIONAL_DEVPROPS;i++)
di->DevicePropertiesSupported[i+di->DevicePropertiesSupported_len] = 0xf000 | i;

Expand Down Expand Up @@ -7479,7 +7476,7 @@ camera_wait_for_event (Camera *camera, int timeout,
/* objectinfo might not even be loaded yet, but this could be 0 / NULL ... but the compare will trigger */
oldparent = ob->oi.ParentObject;
oldstorage = ob->oi.StorageID;
oldfn = strdup(ob->oi.Filename?ob->oi.Filename:"<null>");
C_MEM (oldfn = strdup(ob->oi.Filename?ob->oi.Filename:"<null>"));

ob->flags &= ~PTPOBJECT_OBJECTINFO_LOADED;

Expand Down Expand Up @@ -8569,7 +8566,7 @@ ptp_mtp_parse_metadata (
end = strstr (begin, propname2);
if (!end) continue;
*end = '\0';
content = strdup(begin);
C_MEM (content = strdup(begin));
if (!content) {
free (props);
C_MEM (content);
Expand Down
4 changes: 2 additions & 2 deletions libgphoto2/gphoto2-widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ gp_widget_set_value (CameraWidget *widget, const void *value)
free (widget->value_string);
} else
widget->changed = 1;
widget->value_string = strdup ((char*)value);
C_MEM (widget->value_string = strdup ((char*)value));
return (GP_OK);
case GP_WIDGET_RANGE:
if (widget->value_float != *((float*)value)) {
Expand Down Expand Up @@ -765,7 +765,7 @@ gp_widget_add_choice (CameraWidget *widget, const char *choice)
(widget->type == GP_WIDGET_MENU));

C_MEM (widget->choice = realloc (widget->choice, sizeof(char*)*(widget->choice_count+1)));
widget->choice[widget->choice_count] = strdup(choice);
C_MEM (widget->choice[widget->choice_count] = strdup(choice));
widget->choice_count += 1;
return (GP_OK);
}
Expand Down

0 comments on commit a9091de

Please sign in to comment.