diff --git a/src/kernel/order.c b/src/kernel/order.c index 05a4ac4ba..4cdc20e9f 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -68,29 +68,17 @@ keyword_t getkeyword(const order * ord) * keywords are expanded to their full length. */ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, size_t size) { - char *bufp = sbuffer; order_data *od = NULL; const char * text; keyword_t kwd = ORD_KEYWORD(ord); - int bytes; + sbstring sbs; + sbs_init(&sbs, sbuffer, size); if (ord->command & CMD_QUIET) { - if (size > 0) { - *bufp++ = '!'; - --size; - } - else { - WARN_STATIC_BUFFER(); - } + sbs_strcpy(&sbs, "!"); } if (ord->command & CMD_PERSIST) { - if (size > 0) { - *bufp++ = '@'; - --size; - } - else { - WARN_STATIC_BUFFER(); - } + sbs_strcat(&sbs, "@"); } if (ord->id < 0) { @@ -102,39 +90,19 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si text = OD_STRING(od); } if (kwd != NOKEYWORD) { - if (size > 0) { - const char *str = (const char *)LOC(lang, keyword(kwd)); - assert(str); - if (text) --size; - bytes = (int)str_strlcpy(bufp, str, size); - if (wrptr(&bufp, &size, bytes) != 0) { - WARN_STATIC_BUFFER(); - } - if (text) *bufp++ = ' '; - } - else { - WARN_STATIC_BUFFER(); + const char *str = (const char *)LOC(lang, keyword(kwd)); + assert(str); + sbs_strcat(&sbs, str); + if (text) { + sbs_strcat(&sbs, " "); } } if (text) { - bytes = (int)str_strlcpy(bufp, (const char *)text, size); - if (wrptr(&bufp, &size, bytes) != 0) { - WARN_STATIC_BUFFER(); - if (bufp - sbuffer >= 6) { - bufp -= 6; - while (bufp > sbuffer && (*bufp & 0x80) != 0) { - ++size; - --bufp; - } - memcpy(bufp, "[...]", 6); /* TODO: make sure this only happens in eval_command */ - bufp += 6; - } - } + sbs_strcat(&sbs, text); } if (od) { odata_release(od); } - if (size > 0) *bufp = 0; return sbuffer; } diff --git a/src/util/strings.c b/src/util/strings.c index 06bd43b3a..656a7729b 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -261,5 +261,8 @@ void sbs_strcat(struct sbstring *sbs, const char *str) void sbs_strcpy(struct sbstring *sbs, const char *str) { size_t len = str_strlcpy(sbs->begin, str, sbs->size); - sbs->end += len; + if (len >= sbs->size) { + len = sbs->size - 1; + } + sbs->end = sbs->begin + len; } diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 523d01dbd..8d80183b7 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -105,9 +105,10 @@ static void test_sbstring(CuTest * tc) CuAssertStrEquals(tc, "Hodor", sbs.begin); sbs_strcpy(&sbs, "12345678901234567890"); CuAssertStrEquals(tc, "123456789012345", sbs.begin); + CuAssertPtrEquals(tc, sbs.begin + sbs.size - 1, sbs.end); sbs_strcat(&sbs, "12345678901234567890"); CuAssertStrEquals(tc, "123456789012345", sbs.begin); - CuAssertStrEquals(tc, buffer, sbs.begin); + CuAssertPtrEquals(tc, buffer, sbs.begin); } CuSuite *get_strings_suite(void)