Skip to content

Commit 9f09f0c

Browse files
laurencelundbladeLaurence Lundblade
andauthored
Replace strcpy(), an oft banned function with UsefulOutBuf (#326)
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
1 parent 4ace462 commit 9f09f0c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/qcbor_err_to_str.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* ========================================================================== */
1414

1515
#include "qcbor/qcbor_common.h"
16-
#include <string.h>
16+
#include "UsefulBuf.h"
1717

1818
#define ERR_TO_STR_CASE(errpart) case errpart: return #errpart;
1919

@@ -77,14 +77,17 @@ qcbor_err_to_str(const QCBORError uErr) {
7777
if(uErr >= QCBOR_ERR_FIRST_USER_DEFINED && uErr <= QCBOR_ERR_LAST_USER_DEFINED) {
7878
/* Static buffer is not thread safe, but this is only a diagnostic */
7979
static char buf[20];
80-
strcpy(buf, "USER_DEFINED_");
81-
size_t uEndOffset = strlen(buf);
82-
buf[uEndOffset] = (char)(uErr/100 + '0');
83-
buf[uEndOffset+1] = (char)(((uErr/10) % 10) + '0');
84-
buf[uEndOffset+2] = (char)((uErr % 10 )+ '0');
85-
buf[uEndOffset+3] = '\0';
86-
return buf;
80+
UsefulOutBuf OB;
81+
82+
/* Make NULL-terminated string "USER_DEFINED_NNN" using UsefulOutBuf */
83+
UsefulOutBuf_Init(&OB, UsefulBuf_FROM_BYTE_ARRAY(buf));
84+
UsefulOutBuf_AppendString(&OB, "USER_DEFINED_");
85+
UsefulOutBuf_AppendByte(&OB, (uint8_t)(uErr/100 + '0'));
86+
UsefulOutBuf_AppendByte(&OB, (uint8_t)(((uErr/10) % 10) + '0'));
87+
UsefulOutBuf_AppendByte(&OB, (uint8_t)(((uErr/10) % 10) + '0'));
88+
UsefulOutBuf_AppendByte(&OB, 0x00);
8789

90+
return buf;
8891
} else {
8992
return "Unidentified QCBOR error";
9093
}

0 commit comments

Comments
 (0)