[clinfo] 136/148: Custom, safer, copy to strbuf
Andreas Beckmann
anbe at moszumanska.debian.org
Mon Nov 17 14:09:56 UTC 2014
This is an automated email from the git hooks/post-receive script.
anbe pushed a commit to branch clinfo
in repository clinfo.
commit 6eb23fb08ec267108c4195088ab10a3f051ae7a4
Author: Giuseppe Bilotta <giuseppe.bilotta at gmail.com>
Date: Mon Nov 10 15:36:38 2014 +0100
Custom, safer, copy to strbuf
This also avoids format-security warnings.
---
src/clinfo.c | 20 ++++++++++----------
src/strbuf.h | 31 ++++++++++++++++++++++++++++++-
2 files changed, 40 insertions(+), 11 deletions(-)
diff --git a/src/clinfo.c b/src/clinfo.c
index 6b40cf7..28c894c 100644
--- a/src/clinfo.c
+++ b/src/clinfo.c
@@ -918,7 +918,7 @@ int device_info_cachetype(cl_device_id dev, cl_device_info param, const char *pn
if (!had_error) {
const char * const *ar = (output_mode == CLINFO_HUMAN ?
cache_type_str : cache_type_raw_str);
- sprintf(strbuf, ar[val]);
+ bufcpy(0, ar[val]);
}
show_strbuf(pname, 0);
/* we abuse global strbuf to pass the cache type over to the caller */
@@ -935,7 +935,7 @@ int device_info_lmemtype(cl_device_id dev, cl_device_info param, const char *pna
if (!had_error) {
const char * const *ar = (output_mode == CLINFO_HUMAN ?
lmem_type_str : lmem_type_raw_str);
- sprintf(strbuf, ar[val]);
+ bufcpy(0, ar[val]);
}
show_strbuf(pname, 0);
/* we abuse global strbuf to pass the lmem type over to the caller */
@@ -1321,8 +1321,8 @@ int device_info_fpconf(cl_device_id dev, cl_device_info param, const char *pname
line_pfx, fpstr[i], bool_str[!!(val & cur)]);
} else if (val & cur) {
if (szval > 0)
- szval += sprintf(strbuf + szval, sep);
- szval += sprintf(strbuf + szval, fpstr[i]);
+ szval += bufcpy(szval, sep);
+ szval += bufcpy(szval, fpstr[i]);
}
}
}
@@ -1353,8 +1353,8 @@ int device_info_qprop(cl_device_id dev, cl_device_info param, const char *pname,
line_pfx, qpstr[i], bool_str[!!(val & cur)]);
} else if (val & cur) {
if (szval > 0)
- szval += sprintf(strbuf + szval, sep);
- szval += sprintf(strbuf + szval, qpstr[i]);
+ szval += bufcpy(szval, sep);
+ szval += bufcpy(szval, qpstr[i]);
}
}
if (output_mode == CLINFO_HUMAN && param == CL_DEVICE_QUEUE_PROPERTIES &&
@@ -1385,8 +1385,8 @@ int device_info_execap(cl_device_id dev, cl_device_info param, const char *pname
line_pfx, qpstr[i], bool_str[!!(val & cur)]);
} else if (val & cur) {
if (szval > 0)
- szval += sprintf(strbuf + szval, sep);
- szval += sprintf(strbuf + szval, qpstr[i]);
+ szval += bufcpy(szval, sep);
+ szval += bufcpy(szval, qpstr[i]);
}
}
}
@@ -1447,8 +1447,8 @@ int device_info_svm_cap(cl_device_id dev, cl_device_info param, const char *pnam
line_pfx, scstr[i], bool_str[!!(val & cur)]);
} else if (val & cur) {
if (szval > 0)
- szval += sprintf(strbuf + szval, sep);
- szval += sprintf(strbuf + szval, scstr[i]);
+ szval += bufcpy(szval, sep);
+ szval += bufcpy(szval, scstr[i]);
}
}
}
diff --git a/src/strbuf.h b/src/strbuf.h
index 465d1c1..09dd4fe 100644
--- a/src/strbuf.h
+++ b/src/strbuf.h
@@ -31,6 +31,35 @@ static const char ellip[] = "...";
static void trunc_strbuf(void)
{
- sprintf(strbuf + bufsz - 4, ellip);
+ memcpy(strbuf + bufsz - 4, ellip, 4);
}
+/* copy a string to strbuf, at the given offset,
+ * returning the amount of bytes written (excluding the
+ * closing NULL byte)
+ */
+static inline size_t bufcpy(size_t offset, const char *str)
+{
+ size_t len = strlen(str);
+ size_t maxlen = bufsz - offset - 1;
+ char *dst = strbuf + offset;
+ int trunc = 0;
+ if (bufsz < offset) {
+ fprintf(stderr, "bufcpy overflow copying %s at offset %zu/%zu (%s)\n",
+ str, offset, bufsz, strbuf);
+ maxlen = 0;
+ trunc = 1;
+ }
+ if (len > maxlen) {
+ len = maxlen;
+ trunc = 1;
+ /* TODO enlarge strbuf instead, if maxlen > 0 */
+ }
+ memcpy(dst, str, len);
+ offset += len;
+ if (trunc)
+ trunc_strbuf();
+ else
+ strbuf[offset] = '\0';
+ return len;
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-opencl/clinfo.git
More information about the Pkg-opencl-commits
mailing list