Skip to content

Commit ce1a18e

Browse files
authored
clang-tools: Fix sprintf is deprecated warnings (#120517)
1 parent 956e56f commit ce1a18e

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

clang/tools/c-index-test/c-index-test.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static int parse_remapped_files_with_try(int try_idx,
376376
if (ret)
377377
return ret;
378378

379-
sprintf(opt_name, "-remap-file-%d=", try_idx);
379+
snprintf(opt_name, sizeof(opt_name), "-remap-file-%d=", try_idx);
380380
ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg,
381381
&unsaved_files_try_idx, &num_unsaved_files_try_idx);
382382
if (ret)
@@ -1184,8 +1184,9 @@ static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
11841184
CXString Spelling = clang_getCursorSpelling(Cursor);
11851185
const char *CName = clang_getCString(Name);
11861186
const char *CSpelling = clang_getCString(Spelling);
1187-
char *DefaultSetter = malloc(strlen(CSpelling) + 5);
1188-
sprintf(DefaultSetter, "set%s:", CSpelling);
1187+
size_t Len = strlen(CSpelling) + 5;
1188+
char *DefaultSetter = malloc(Len);
1189+
snprintf(DefaultSetter, Len, "set%s:", CSpelling);
11891190
DefaultSetter[3] &= ~(1 << 5); /* Make uppercase */
11901191
if (CName && strcmp(CName, DefaultSetter)) {
11911192
printf(" (setter=%s)", CName);
@@ -3545,19 +3546,20 @@ static CXIdxClientContainer makeClientContainer(CXClientData *client_data,
35453546
char *newStr;
35463547
CXIdxClientFile file;
35473548
unsigned line, column;
3548-
3549+
size_t len;
3550+
35493551
name = info->name;
35503552
if (!name)
35513553
name = "<anon-tag>";
35523554

35533555
clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
35543556

3555-
node =
3556-
(IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) +
3557-
digitCount(line) + digitCount(column) + 2);
3557+
len = sizeof(IndexDataStringList) + strlen(name) + digitCount(line) +
3558+
digitCount(column) + 2;
3559+
node = (IndexDataStringList *)malloc(len);
35583560
assert(node);
35593561
newStr = node->data;
3560-
sprintf(newStr, "%s:%d:%d", name, line, column);
3562+
snprintf(newStr, len, "%s:%d:%d", name, line, column);
35613563

35623564
/* Remember string so it can be freed later. */
35633565
index_data = (IndexData *)client_data;

0 commit comments

Comments
 (0)