summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2012-05-06 12:41:17 +0200
committerLars-Dominik Braun <lars@6xq.net>2012-05-06 12:45:40 +0200
commit0690a53ced01a19837a91c375cc9c44601d43066 (patch)
tree41703cd924f1468d60e96778a31fa78de5db5e62 /src
parentc894f853294db4b3b2b9395290678cde2b1c0373 (diff)
downloadpianobar-0690a53ced01a19837a91c375cc9c44601d43066.tar.gz
pianobar-0690a53ced01a19837a91c375cc9c44601d43066.tar.bz2
pianobar-0690a53ced01a19837a91c375cc9c44601d43066.zip
piano: Fix insane strcpy
Diffstat (limited to 'src')
-rw-r--r--src/libpiano/response.c40
1 files changed, 29 insertions, 11 deletions
diff --git a/src/libpiano/response.c b/src/libpiano/response.c
index 0379daf..c3bfd6c 100644
--- a/src/libpiano/response.c
+++ b/src/libpiano/response.c
@@ -49,6 +49,30 @@ static void PianoJsonParseStation (json_object *j, PianoStation_t *s) {
"isQuickMix"));
}
+/* concat strings
+ * @param destination
+ * @param source string
+ * @param destination size
+ */
+static void PianoStrpcat (char * restrict dest, const char * restrict src,
+ size_t len) {
+ /* skip to end of string */
+ while (*dest != '\0' && len > 1) {
+ ++dest;
+ --len;
+ }
+
+ /* append until source exhaused or destination full */
+ while (*src != '\0' && len > 1) {
+ *dest = *src;
+ ++dest;
+ ++src;
+ --len;
+ }
+
+ *dest = '\0';
+}
+
/* parse xml response and update data structures/return new data structure
* @param piano handle
* @param initialized request (expects responseData to be a NUL-terminated
@@ -490,8 +514,7 @@ PianoReturn_t PianoResponse (PianoHandle_t *ph, PianoRequest_t *req) {
case PIANO_REQUEST_EXPLAIN: {
/* explain why song was selected */
PianoRequestDataExplain_t *reqData = req->data;
- const size_t strSize = 1024;
- size_t pos = 0;
+ const size_t strSize = 768;
assert (reqData != NULL);
@@ -502,24 +525,19 @@ PianoReturn_t PianoResponse (PianoHandle_t *ph, PianoRequest_t *req) {
sizeof (*reqData->retExplain));
strncpy (reqData->retExplain, "We're playing this track "
"because it features ", strSize);
- pos = strlen (reqData->retExplain);
for (size_t i=0; i < json_object_array_length (explanations); i++) {
json_object *e = json_object_array_get_idx (explanations,
i);
const char *s = json_object_get_string (
json_object_object_get (e, "focusTraitName"));
- strncpy (&reqData->retExplain[pos], s, strSize-pos-1);
- pos += strlen (s);
+ PianoStrpcat (reqData->retExplain, s, strSize);
if (i < json_object_array_length (explanations)-2) {
- strncpy (&reqData->retExplain[pos], ", ", strSize-pos-1);
- pos += 2;
+ PianoStrpcat (reqData->retExplain, ", ", strSize);
} else if (i == json_object_array_length (explanations)-2) {
- strncpy (&reqData->retExplain[pos], " and ", strSize-pos-1);
- pos += 5;
+ PianoStrpcat (reqData->retExplain, " and ", strSize);
} else {
- strncpy (&reqData->retExplain[pos], ".", strSize-pos-1);
- pos += 1;
+ PianoStrpcat (reqData->retExplain, ".", strSize);
}
}
}