Skip to content

Commit

Permalink
Win: move to safe version of sscanf
Browse files Browse the repository at this point in the history
sscanf is known to cause problems on many architectures, so on Windows,
move to the safer version sscanf_s, downside - this does require
different arguments sometimes.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed May 1, 2020
1 parent 2e400fa commit 19522e0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
#ifdef _MSC_BUILD
#define inline __inline
#define iio_snprintf sprintf_s
#define iio_sscanf sscanf_s
#else
#define iio_snprintf snprintf
#define iio_sscanf sscanf
#endif

#ifdef _WIN32
Expand Down
2 changes: 1 addition & 1 deletion iiod-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ static int iiod_client_read_mask(struct iiod_client *client,
IIO_DEBUG("Reading mask\n");

for (i = words, ptr = buf; i > 0; i--) {
sscanf(ptr, "%08" PRIx32, &mask[i - 1]);
iio_sscanf(ptr, "%08" PRIx32, &mask[i - 1]);
IIO_DEBUG("mask[%lu] = 0x%08" PRIx32 "\n",
(unsigned long)(i - 1), mask[i - 1]);

Expand Down
16 changes: 14 additions & 2 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -1230,12 +1230,24 @@ static int handle_protected_scan_element_attr(struct iio_channel *chn,
char endian, sign;

if (strchr(buf, 'X')) {
sscanf(buf, "%ce:%c%u/%uX%u>>%u", &endian, &sign,
iio_sscanf(buf, "%ce:%c%u/%uX%u>>%u",
#ifdef _MSC_BUILD
&endian, sizeof(endian),
&sign, sizeof(sign),
#else
&endian, &sign,
#endif
&chn->format.bits, &chn->format.length,
&chn->format.repeat, &chn->format.shift);
} else {
chn->format.repeat = 1;
sscanf(buf, "%ce:%c%u/%u>>%u", &endian, &sign,
iio_sscanf(buf, "%ce:%c%u/%u>>%u",
#ifdef _MSC_BUILD
&endian, sizeof(endian),
&sign, sizeof(sign),
#else
&endian, &sign,
#endif
&chn->format.bits, &chn->format.length,
&chn->format.shift);
}
Expand Down
2 changes: 1 addition & 1 deletion network.c
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ static ssize_t network_read_mask(struct iio_network_io_context *io_ctx,
if (ret < 0)
return ret;

sscanf(buf, "%08x", &mask[i - 1]);
iio_sscanf(buf, "%08x", &mask[i - 1]);
IIO_DEBUG("mask[%lu] = 0x%x\n",
(unsigned long)(i - 1), mask[i - 1]);
}
Expand Down
14 changes: 12 additions & 2 deletions xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,24 @@ static void setup_scan_element(struct iio_channel *chn, xmlNode *n)
} else if (!strcmp(name, "format")) {
char e, s;
if (strchr(content, 'X')) {
sscanf(content, "%ce:%c%u/%uX%u>>%u", &e, &s,
iio_sscanf(content, "%ce:%c%u/%uX%u>>%u",
#ifdef _MSC_BUILD
&e, sizeof(e), &s, sizeof(s),
#else
&e, &s,
#endif
&chn->format.bits,
&chn->format.length,
&chn->format.repeat,
&chn->format.shift);
} else {
chn->format.repeat = 1;
sscanf(content, "%ce:%c%u/%u>>%u", &e, &s,
iio_sscanf(content, "%ce:%c%u/%u>>%u",
#ifdef _MSC_BUILD
&e, sizeof(e), &s, sizeof(s),
#else
&e, &s,
#endif
&chn->format.bits,
&chn->format.length,
&chn->format.shift);
Expand Down

0 comments on commit 19522e0

Please sign in to comment.