Skip to content

Conversation

@arsenm
Copy link
Contributor

@arsenm arsenm commented Dec 19, 2024

No description provided.

@arsenm arsenm marked this pull request as ready for review December 19, 2024 04:17
Copy link
Contributor Author

arsenm commented Dec 19, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Dec 19, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 19, 2024

@llvm/pr-subscribers-clang

Author: Matt Arsenault (arsenm)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/120517.diff

1 Files Affected:

  • (modified) clang/tools/c-index-test/c-index-test.c (+9-8)
diff --git a/clang/tools/c-index-test/c-index-test.c b/clang/tools/c-index-test/c-index-test.c
index b48f44950ab754..46e534e6ee4f1e 100644
--- a/clang/tools/c-index-test/c-index-test.c
+++ b/clang/tools/c-index-test/c-index-test.c
@@ -374,7 +374,7 @@ static int parse_remapped_files_with_try(int try_idx,
   if (ret)
     return ret;
 
-  sprintf(opt_name, "-remap-file-%d=", try_idx);
+  snprintf(opt_name, sizeof(opt_name), "-remap-file-%d=", try_idx);
   ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg,
       &unsaved_files_try_idx, &num_unsaved_files_try_idx);
   if (ret)
@@ -1182,8 +1182,9 @@ static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
       CXString Spelling = clang_getCursorSpelling(Cursor);
       const char *CName = clang_getCString(Name);
       const char *CSpelling = clang_getCString(Spelling);
-      char *DefaultSetter = malloc(strlen(CSpelling) + 5);
-      sprintf(DefaultSetter, "set%s:", CSpelling);
+      size_t Len = strlen(CSpelling) + 5;
+      char *DefaultSetter = malloc(Len);
+      snprintf(DefaultSetter, Len, "set%s:", CSpelling);
       DefaultSetter[3] &= ~(1 << 5); /* Make uppercase */
       if (CName && strcmp(CName, DefaultSetter)) {
         printf(" (setter=%s)", CName);
@@ -3526,19 +3527,19 @@ static CXIdxClientContainer makeClientContainer(CXClientData *client_data,
   char *newStr;
   CXIdxClientFile file;
   unsigned line, column;
-  
+  size_t len;
+
   name = info->name;
   if (!name)
     name = "<anon-tag>";
 
   clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
 
-  node =
-      (IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) +
-                                    digitCount(line) + digitCount(column) + 2);
+  len = strlen(name) + digitCount(line) + digitCount(column) + 2;
+  node = (IndexDataStringList *)malloc(sizeof(IndexDataStringList) + len);
   assert(node);
   newStr = node->data;
-  sprintf(newStr, "%s:%d:%d", name, line, column);
+  snprintf(newStr, len, "%s:%d:%d", name, line, column);
 
   /* Remember string so it can be freed later. */
   index_data = (IndexData *)client_data;

@arsenm arsenm force-pushed the users/arsenm/clang-tools-fix-sprintf-deprecated-warnings branch from 5936908 to dfc338a Compare March 3, 2025 17:04
@arsenm arsenm merged commit ce1a18e into main Mar 4, 2025
11 checks passed
@arsenm arsenm deleted the users/arsenm/clang-tools-fix-sprintf-deprecated-warnings branch March 4, 2025 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants