Skip to content

Commit e428421

Browse files
committed
Everything uses osnprintf instead of snprintf
osnprintf has offset and is safe
1 parent 476c5bf commit e428421

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/data.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int ptp_get_data_size(void *d, int type) {
5656
return 0;
5757
}
5858

59-
int ptp_parse_data(void *d, int type, int *out) {
59+
int ptp_parse_data_u32(void *d, int type, int *out) {
6060
uint8_t a;
6161
uint16_t b;
6262
uint32_t c;
@@ -90,7 +90,7 @@ int ptp_parse_prop_value(struct PtpRuntime *r) {
9090
return -1;
9191
}
9292

93-
static int parse_data_data_or_u32(uint8_t *d, int type, uint32_t *u32, void **data) {
93+
int parse_data_data_or_u32(uint8_t *d, int type, uint32_t *u32, void **data) {
9494
int size;
9595
uint32_t length32;
9696
uint8_t length8;
@@ -102,7 +102,7 @@ static int parse_data_data_or_u32(uint8_t *d, int type, uint32_t *u32, void **da
102102
case PTP_TC_UINT16:
103103
case PTP_TC_INT32:
104104
case PTP_TC_UINT32:
105-
return ptp_parse_data(d, type, u32);
105+
return ptp_parse_data_u32(d, type, u32);
106106
case PTP_TC_INT64:
107107
case PTP_TC_UINT64:
108108
(*data) = malloc(8);
@@ -145,9 +145,9 @@ int ptp_parse_prop_desc(struct PtpRuntime *r, struct PtpPropDesc *oi) {
145145
d += ptp_read_u8(d, &oi->form_type);
146146

147147
if (oi->form_type == PTP_RangeForm) {
148-
d += ptp_parse_data(d, oi->data_type, &oi->range_form.min);
149-
d += ptp_parse_data(d, oi->data_type, &oi->range_form.max);
150-
d += ptp_parse_data(d, oi->data_type, &oi->range_form.step);
148+
d += ptp_parse_data_u32(d, oi->data_type, &oi->range_form.min);
149+
d += ptp_parse_data_u32(d, oi->data_type, &oi->range_form.max);
150+
d += ptp_parse_data_u32(d, oi->data_type, &oi->range_form.step);
151151
} else if (oi->form_type == PTP_EnumerationForm) {
152152
uint16_t num_values = 0;
153153
d += ptp_read_u16(d, &num_values);
@@ -318,20 +318,20 @@ const char *eval_protection(int code) {
318318

319319
int ptp_object_info_json(struct PtpObjectInfo *so, char *buffer, int max) {
320320
int curr = sprintf(buffer, "{");
321-
curr += snprintf(buffer + curr, max - curr, "\"storage_id\": %u,", so->storage_id);
322-
curr += snprintf(buffer + curr, max - curr, "\"compressedSize\": %u,", so->compressed_size);
323-
curr += snprintf(buffer + curr, max - curr, "\"parent\": %u,", so->parent_obj);
324-
curr += snprintf(buffer + curr, max - curr, "\"format\": \"%s\",", eval_obj_format(so->obj_format));
325-
curr += snprintf(buffer + curr, max - curr, "\"format_int\": %u,", so->obj_format);
326-
curr += snprintf(buffer + curr, max - curr, "\"protection\": \"%s\",", eval_protection(so->protection));
327-
curr += snprintf(buffer + curr, max - curr, "\"filename\": \"%s\",", so->filename);
321+
curr += osnprintf(buffer, curr, max, "\"storage_id\": %u,", so->storage_id);
322+
curr += osnprintf(buffer, curr, max, "\"compressedSize\": %u,", so->compressed_size);
323+
curr += osnprintf(buffer, curr, max, "\"parent\": %u,", so->parent_obj);
324+
curr += osnprintf(buffer, curr, max, "\"format\": \"%s\",", eval_obj_format(so->obj_format));
325+
curr += osnprintf(buffer, curr, max, "\"format_int\": %u,", so->obj_format);
326+
curr += osnprintf(buffer, curr, max, "\"protection\": \"%s\",", eval_protection(so->protection));
327+
curr += osnprintf(buffer, curr, max, "\"filename\": \"%s\",", so->filename);
328328
if (so->compressed_size != 0) {
329-
curr += snprintf(buffer + curr, max - curr, "\"imgWidth\": %u,", so->img_width);
330-
curr += snprintf(buffer + curr, max - curr, "\"imgHeight\": %u,", so->img_height);
329+
curr += osnprintf(buffer, curr, max, "\"imgWidth\": %u,", so->img_width);
330+
curr += osnprintf(buffer, curr, max, "\"imgHeight\": %u,", so->img_height);
331331
}
332-
curr += snprintf(buffer + curr, max - curr, "\"dateCreated\": \"%s\",", so->date_created);
333-
curr += snprintf(buffer + curr, max - curr, "\"dateModified\": \"%s\"", so->date_modified);
334-
curr += snprintf(buffer + curr, max - curr, "}");
332+
curr += osnprintf(buffer, curr, max, "\"dateCreated\": \"%s\",", so->date_created);
333+
curr += osnprintf(buffer, curr, max, "\"dateModified\": \"%s\"", so->date_modified);
334+
curr += osnprintf(buffer, curr, max, "}");
335335

336336
return curr;
337337
}
@@ -572,6 +572,7 @@ int ptp_eos_events_json(struct PtpRuntime *r, char *buffer, int max) {
572572
return curr;
573573
}
574574

575+
// TODO: move to fudge
575576
int ptp_fuji_get_init_info(struct PtpRuntime *r, struct PtpFujiInitResp *resp) {
576577
void *dat = r->data + 12;
577578

0 commit comments

Comments
 (0)