无效写入——Valgrind

2023-12-23

您好,我在我的 c 程序中遇到了 munmap_chunk(): invalid point: 错误。

主要问题是......我什至不确定指针可能变得无效的所有方式是什么。我已经检查了我的代码中是否有没有足够空间调用的字符串,但没有发现任何看起来会超出范围的情况!

相关代码如下(我认为无论如何都是相关代码)

//Takes in a username and suggests friends of friends who are the opposite sex as friends of username
395 void suggest_friends(char* username, FILE* out) {
396     //printf("ENTER suggest_friends\n");
397     to_lowercase(username);
398 
399     if (check_username(username) == 0) {
400 
401         struct user_node *user = search_username(username);
402         if (user != NULL) {
403 
404             struct friend_node *a_friend = user->a_friend;
405             struct friend_node *friends_friend = NULL;
406             struct friend_node *temp_friends_friend = NULL;
407 
408             struct friend_suggest_node *list = NULL;
409             struct friend_suggest_node *list_it = NULL;
410             struct friend_suggest_node *new_suggest = NULL;
411             int friend_suggest_switch = -1;
412 
413             int print_string_size = 50;
414             int print_string_len = 0;
415             char* print_string = calloc(print_string_size + 1, sizeof(char));
416             char* user_string = NULL;
417             int num_suggestions = 0;
418             int num_mutual_friends = 0;
419             int max_friends = 0;
420 
421             //Iterate over all friends
422             while (a_friend != NULL) {
423 
424                 friends_friend = a_friend->user->a_friend;
425                 //Does friend have friends of opposite sex that I'm not friends with?
426                 //Iterate over friend's friends
427                 while (friends_friend != NULL) {
428 
429                     num_mutual_friends = 0;
430                     //mutual friend found
431                         //Different gender, and not friends
432                     if (friends_friend->user->gender != user->gender && are_friends(friends_friend->u    ........ser, user) != 0) {
433 
434                         //are there are elements in the suggested friends list yet??
435                         if (list == NULL) {
436 
437                             new_suggest = malloc(sizeof(struct friend_suggest_node));
438                             new_suggest->user = friends_friend->user;
439                             new_suggest->next = NULL;
440                             list = new_suggest;
441                             friend_suggest_switch = 0;
442                         }
443                         //there are already elements
444                         else {
445 
446                             friend_suggest_switch = 0;
447                             //Loop over suggested friends, to check if friends friend already found
448                             list_it = list;
449                             while (list_it != NULL) {
450 
451                                 //if the user is already in the suggested list
452                                 if (list_it->user == friends_friend->user) {
453                                     friend_suggest_switch = -1;
454                                     break;
455                                 }
456                                 list_it = list_it->next;
457                             }
458 
459                             //if the friend to suggest is a new suggestion
460                             if (friend_suggest_switch == 0) {
461 
462                                 //add friend to suggest to the front of the list
462                                 //add friend to suggest to the front of the list
463                                 new_suggest = malloc(sizeof(struct friend_suggest_node));
464                                 new_suggest->user = friends_friend->user;
465                                 new_suggest->next = list;
466                                 list = new_suggest;
467                             }
468                         }
469 
470                         //if the friend found was new
471                         if (friend_suggest_switch == 0) {
472 
473                             //INTENTION? LOOP OF THE FRIEND OF A FRIEND'S FRIEND LIST!?
474                             //Loop over the remainder of the user's friends's, friend list
475                                 //whom is about to be suggested as a mutual friend
476                             temp_friends_friend = friends_friend->user->a_friend;
477                             while (temp_friends_friend != NULL) {
478 
479                                 //if user is found who is a mutual friend with user
480                                 if (are_friends(temp_friends_friend->user, user) == 0) {
481 
482                                     num_mutual_friends++;
483                                 }
484 
485                                 temp_friends_friend = temp_friends_friend->next_friend;
486                             }
487 
488                             //if more mutual friends then previous choice,
489                                 //set user_string equal to this user now
490                             if (num_mutual_friends > max_friends) {
491                                 max_friends = num_mutual_friends;
492                             }
493 
494                             //get string for user
495                             user_string = get_user_string(friends_friend->user);
496                             num_suggestions++;
497                             //+3 for \0 and ', '
498                             print_string_len = strlen(user_string) + 3;
498                             print_string_len = strlen(user_string) + 3;
499 
500                             //if length exceeds size of string
501                             if (print_string_len > print_string_size) {
502 
503                                 while (print_string_len >= print_string_size) {
504                                     print_string_size *= 2;
505                                 }
506 
507                                 char* temp_string = calloc(print_string_size + 1, sizeof(char));
508                                 strcpy(temp_string, print_string);
509                                 free(print_string);
510                                 print_string = temp_string;
511                                 temp_string = NULL;
512                             }
513 
514                             //add ", " fot string for formatting
515                             if (strlen(print_string) > 0) {
516                                 strcat(print_string, ", \0");
517                                 //TBR
518                                 //printf("AFTER TACKING ON COMMA!\n");
519                             }
520                             strcat(print_string, user_string);
521                             //TBR
522                             //fprintf(out, "before fail 111\n");
523                             //fprintf(out, "user_string is  %s\n", user_string);
524                             //fprintf(out, "user_string ptr is %p\n", user_string);
525                             free(user_string);
526                             //TBR
527                             //fprintf(out, "after fail 111???\n");
528                             user_string = NULL;
529                         }
530                     }
531 
532                     friends_friend = friends_friend->next_friend;
533                 }
534 
535                 a_friend = a_friend->next_friend;
536             }
537 
538             if (num_suggestions != 0) {
539                 fprintf(out, "%s may know following people because they have %d mutual friend(s):\n%s    ........\n", username, max_friends, print_string);
540             }
541             else {
542                 fprintf(out, "Sorry, there are no friend suggestions for %s.\n", username);
543             }
544 
545             free(print_string);
546             print_string = NULL;
547         }
548         else {
549             fprintf(out, "User %s does not exist. Please try again.\n", username);
550         }
551     }
552     else {
553         fprintf(out, "%s username is not a valid username\n", username);
554     }
555     //printf("EXIT suggest_friends\n");
556 }



//Takes a user_node and returns a char* to a string holding the user's info
771     //in the format name/age/gender/location
772 char* get_user_string(struct user_node *user) {
773     char age[5];
774     sprintf(age, "%d", (user->age));
775     char* gender = NULL;
776 
777     //Female
778     if (user->gender == 0) {
779         gender = "female\0";
780     }
781     //male
782     else {
783         gender = "male\0";
784     }
785 
786     //allocate memory for length of location, name, age, and gender + 3 '/'s + \0
787         //+20 for good measure!!!
788     char* user_string = NULL;
789     user_string = calloc((strlen(user->name) + strlen(user->location) + strlen(age) + strlen(gender)     ........+ 4 + 20), sizeof(char));
790     strcat(user_string, user->name);
791     strcat(user_string, "/");
792     strcat(user_string, age);
793     strcat(user_string, "/");
794     strcat(user_string, gender);
795     strcat(user_string, "/");
796     strcat(user_string, user->location);
797     return user_string;
798 }

当我通过 valgrind 运行它时,我得到以下输出:

==10158== Memcheck, a memory error detector
==10158== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==10158== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==10158== Command: ./social_network -f crash_tester.txt crash_test_output.txt
==10158== 
==10158== Invalid write of size 1
==10158==    at 0x402C36B: strcat (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==10158==    by 0x804A434: suggest_friends (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804B25A: command_read (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049460: read_args_file (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049294: switch_parsing (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804ACF7: main (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==  Address 0x41f12bb is 0 bytes after a block of size 51 alloc'd
==10158==    at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==10158==    by 0x804A1F3: suggest_friends (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804B25A: command_read (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049460: read_args_file (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049294: switch_parsing (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804ACF7: main (in /home/ethan/cs2506/comp-org-3/social_network)
==10158== 
==10158== Invalid write of size 1
==10158==    at 0x402C390: strcat (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==10158==    by 0x804A434: suggest_friends (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804B25A: command_read (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049460: read_args_file (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049294: switch_parsing (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804ACF7: main (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==  Address 0x41f12cf is not stack'd, malloc'd or (recently) free'd
==10158== 
==10158== Invalid read of size 1
==10158==    at 0x4089E29: vfprintf (vfprintf.c:1630)
==10158==    by 0x4091EBE: fprintf (fprintf.c:33)
==10158==    by 0x804B25A: command_read (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049460: read_args_file (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049294: switch_parsing (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804ACF7: main (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==  Address 0x41f12bb is 0 bytes after a block of size 51 alloc'd
==10158==    at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==10158==    by 0x804A1F3: suggest_friends (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804B25A: command_read (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049460: read_args_file (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049294: switch_parsing (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804ACF7: main (in /home/ethan/cs2506/comp-org-3/social_network)
==10158== 
==10158== Invalid read of size 4
==10158==    at 0x40C40BC: __GI_mempcpy (mempcpy.S:60)
==10158==    by 0x40B6769: _IO_file_xsputn@@GLIBC_2.1 (fileops.c:1350)
==10158==    by 0x4089E01: vfprintf (vfprintf.c:1630)
==10158==    by 0x4091EBE: fprintf (fprintf.c:33)
==10158==    by 0x804B25A: command_read (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049460: read_args_file (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049294: switch_parsing (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804ACF7: main (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==  Address 0x41f12bb is 0 bytes after a block of size 51 alloc'd
==10158==    at 0x402A5E6: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==10158==    by 0x804A1F3: suggest_friends (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804B25A: command_read (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049460: read_args_file (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x8049294: switch_parsing (in /home/ethan/cs2506/comp-org-3/social_network)
==10158==    by 0x804ACF7: main (in /home/ethan/cs2506/comp-org-3/social_network)
==10158== 

我相信问题就在这里:

print_string_len = strlen(user_string) + 3;

//if length exceeds size of string
if (print_string_len > print_string_size) {

    while (print_string_len >= print_string_size) {
        print_string_size *= 2;
    }

    char* temp_string = calloc(print_string_size + 1, sizeof(char));
    strcpy(temp_string, print_string);
    free(print_string);
    print_string = temp_string;
    temp_string = NULL;
}

//add ", " fot string for formatting
if (strlen(print_string) > 0) {
    strcat(print_string, ", \0");
}
strcat(print_string, user_string);

当您尝试根据缓冲区限制预先检查最终结果长度时,最终结果长度计算不正确。您忘记包含缓冲区中预先存在的内容;例如strlen(print_string).

所以我认为你需要改变:

print_string_len = strlen(user_string) + 3;

to:

print_string_len = strlen(print_string) + strlen(user_string) + 3;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无效写入——Valgrind 的相关文章

  • 如何检查图像对象与资源中的图像对象是否相同?

    所以我试图创建一个简单的程序 只需在单击图片框中更改图片即可 我目前只使用两张图片 所以我的图片框单击事件函数的代码 看起来像这样 private void pictureBox1 Click object sender EventArgs
  • 访问私人成员[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 通过将类的私有成员转换为 void 指针 然后转换为结构来访问类的私有成员是否合适 我认为我无权修改包含我需要访问的数据成员的类 如果不道德 我
  • Qt-Qlist 检查包含自定义类

    有没有办法覆盖加载自定义类的 Qt QList 的比较机制 即在 java 中你只需要重写一个比较方法 我有一个带有我的自定义类模型的 QList QList
  • WPF 中的调度程序和异步等待

    我正在尝试学习 WPF C 中的异步编程 但我陷入了异步编程和使用调度程序的困境 它们是不同的还是在相同的场景中使用 我愿意简短地回答这个问题 以免含糊不清 因为我知道我混淆了 WPF 中的概念和函数 但还不足以在功能上正确使用它 我在这里
  • 将目录压缩为单个文件的方法有哪些

    不知道怎么问 所以我会解释一下情况 我需要存储一些压缩文件 最初的想法是创建一个文件夹并存储所需数量的压缩文件 并创建一个文件来保存有关每个压缩文件的数据 但是 我不被允许创建许多文件 只能有一个 我决定创建一个压缩文件 其中包含有关进一步
  • C 预处理器库

    我的任务是开发源分析工具C程序 并且我需要在分析本身之前预处理代码 我想知道什么是最好的图书馆 我需要一些重量轻 便于携带的东西 与其推出自己的 为什么不使用cpp这是的一部分gcc suite http gcc gnu org onlin
  • 使用 System.Text.Json 即时格式化 JSON 流

    我有一个未缩进的 Json 字符串 例如 hash 123 id 456 我想缩进字符串并将其序列化为 JSON 文件 天真地 我可以使用缩进字符串Newtonsoft如下 using Newtonsoft Json Linq JToken
  • 如何返回 json 结果并将 unicode 字符转义为 \u1234

    我正在实现一个返回 json 结果的方法 例如 public JsonResult MethodName Guid key var result ApiHelper GetData key Data is stored in db as v
  • Github Action 在运行可执行文件时卡住

    我正在尝试设置运行google tests on a C repository using Github Actions正在运行的Windows Latest 构建过程完成 但是当运行测试时 它被卡住并且不执行从生成的可执行文件Visual
  • 如何衡量两个字符串之间的相似度? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 给定两个字符串text1 and text2 public SOMEUSABLERETURNTYPE Compare string t
  • 插入记录后如何从SQL Server获取Identity值

    我在数据库中添加一条记录identity价值 我想在插入后获取身份值 我不想通过存储过程来做到这一点 这是我的代码 SQLString INSERT INTO myTable SQLString Cal1 Cal2 Cal3 Cal4 SQ
  • C++ fmt 库,仅使用格式说明符格式化单个参数

    使用 C fmt 库 并给定一个裸格式说明符 有没有办法使用它来格式化单个参数 example std string str magic format 2f 1 23 current method template
  • 如何让Gtk+窗口背景透明?

    我想让 Gtk 窗口的背景透明 以便只有窗口中的小部件可见 我找到了一些教程 http mikehearn wordpress com 2006 03 26 gtk windows with alpha channels https web
  • 将文本叠加在图像背景上并转换为 PDF

    使用 NET 我想以编程方式创建一个 PDF 它仅包含一个背景图像 其上有两个具有不同字体和位置的标签 我已阅读过有关现有 PDF 库的信息 但不知道 如果适用 哪一个对于如此简单的任务来说最简单 有人愿意指导我吗 P D 我不想使用生成的
  • WCF:将随机数添加到 UsernameToken

    我正在尝试连接到用 Java 编写的 Web 服务 但有些东西我无法弄清楚 使用 WCF 和 customBinding 几乎一切似乎都很好 除了 SOAP 消息的一部分 因为它缺少 Nonce 和 Created 部分节点 显然我错过了一
  • 为什么 C# Math.Ceiling 向下舍入?

    我今天过得很艰难 但有些事情不太对劲 在我的 C 代码中 我有这样的内容 Math Ceiling decimal this TotalRecordCount this PageSize Where int TotalRecordCount
  • const、span 和迭代器的问题

    我尝试编写一个按索引迭代容器的迭代器 AIt and a const It两者都允许更改容器的内容 AConst it and a const Const it两者都禁止更改容器的内容 之后 我尝试写一个span
  • x86 上未对齐的指针

    有人可以提供一个示例 将指针从一种类型转换为另一种类型由于未对齐而失败吗 在评论中这个答案 https stackoverflow com questions 544928 reading integer size bytes from a
  • ASP.NET MVC 6 (ASP.NET 5) 中的 Application_PreSendRequestHeaders 和 Application_BeginRequest

    如何在 ASP NET 5 MVC6 中使用这些方法 在 MVC5 中 我在 Global asax 中使用了它 现在呢 也许是入门班 protected void Application PreSendRequestHeaders obj
  • 防止索引超出范围错误

    我想编写对某些条件的检查 而不必使用 try catch 并且我想避免出现 Index Out of Range 错误的可能性 if array Element 0 Object Length gt 0 array Element 1 Ob

随机推荐

  • MySQL 获取两个用户之间的对话

    我有一个名为 private messages 的 SQL 表 其中包含字段 id from to message stamp 标记字段对应于消息的日期 那么我需要什么查询 1 获取两个用户之间的对话 按日期排序 我已经尝试过查询 SELE
  • Hive 中的 Presto UNNEST 函数相当于什么

    急板有一个UNNEST函数分解由数组组成的列 Hive 有类似的吗 请参阅文档UNNEST急速功能here https prestodb io docs current sql select html Use lateral view ou
  • 使用 DTO 时,Automapper 和 Nhibernate 反映正在更新的域对象中 DTO 子集合的更改

    我对这个设计不是很熟悉 但我希望得到一些指导 我有一个后端服务 它将 DTO 发送到 WPF 智能客户端 在 WPF 智能客户端上 用户将更改 删除和修改项目 然后将更改发送回 客户端 gt 服务器 举个例子 目前我正在处理客户详细信息表单
  • 无法让 dokka 在 gradle/android 项目上生成 kotlin 文档

    我正在关注 gradle 插件部分https github com Kotlin dokka https github com Kotlin dokka 我还尝试了 dokka gradle example 中的示例https github
  • Spring结果集提取器

    如何使用ResultSetExtractor从数据库检索数据 这里我使用oracle 10g作为后端 如果从员工表中搜索员工详细信息 哪个更好ResultSetExtractor or RowMapper 从 java 8 开始 还可以使用
  • 无继承的 OOP 重用:这在“现实世界”中有多实用?

    本文描述了一种我觉得很有趣的 OOP 方法 如果对象存在为 封装和通信 通过消息 如果代码重用了怎么办 与继承无关 但是 使用组合 委托 甚至 老式的辅助对象或任何 程序员认为合适的技术 本体并没有消失 但它 与实现分离 最令我震惊的是无需
  • 如何使用 Spring 的 JDBCTemplate 有效执行 IN() SQL 查询?

    我想知道是否有一种更优雅的方法来使用 Spring 的 JDBCTemplate 进行 IN 查询 目前我正在做类似的事情 StringBuilder jobTypeInClauseBuilder new StringBuilder for
  • JavaFX 没有 TouchEvents

    在我的 Windows Surface Go 2 平板电脑上 我无法检索任何 多点触控 触摸事件 我尝试了几个 Java FX 版本 即使使用 JavaFX 17 及更早版本 也不会生成 TouchEvents 事件 仅生成鼠标事件 是否需
  • 每10秒自我更新一次的小部件Handler.postDelayed问题

    我正在尝试使工作成为 Android 小部件中的自我更新功能 就像每 10 秒更改它的两个 TextView 一样简单 理想的解决方案是使其类似于精灵小部件 新闻和天气 到目前为止 它工作正常 它通过 Handler postDelayed
  • Pycharm 设置 Mysql 数据库驱动程序

    我正在尝试在 pycharm 中设置 mysql 数据库连接 我已经创建了架构并且它可以在 django 等中工作 我试图将数据源直接导入到 pycharm 但出现以下错误 Connection to Exception in thread
  • 为什么 smartcast 不能处理这种情况?

    我有一些与此结构类似的代码 private fun test Double val a Double 15 0 val b Double 20 0 return if a null b null 0 else if a null b nul
  • 我的主函数反汇编中所有这些奇怪的汇编指令是什么?

    所以我有这个主要功能 它会产生很多奇怪的指令 我使用的是 Visual Studio 2019 并且处于调试模式 因此禁用了优化 这些指令是做什么的 int main 00D340E0 push ebp 00D340E1 mov ebp e
  • 使用自定义 Comparer 的 OrderBy 的 Linq 语法

    对于带有自定义排序比较器的任何给定 Linq 表达式 有两种格式 Format 1 var query source Select x gt new x someProperty x otherProperty OrderBy x gt x
  • jQuery fadeIn '慢' 立即出现

    我试图做到这一点 以便当您单击链接时 它会删除一个 div 带有一些段落和文本 并插入另一个 div 带有一些段落和一些文本 我正在使用 jQuery 来淡入和淡出它们 当您单击链接时 原始 div 会淡出 然后我有一个 switch ca
  • 如何从 GooglePicker 上选定的文件中获取 blob

    我正在使用 GooglePicker 和 React 我得到的结果是一个对象数组 id 1 m serviceId docs mimeType image jpeg name name jpg description type photo
  • 带有纹理背景的 CSS 之字形边框

    我一直在研究带有锯齿形边框的标题 一种方法是使用图像来制作之字形效果 1 有没有办法在CSS中创建一个实用的跨浏览器之字形边框而不使用图像 我还尝试在该标题上放置一个延伸到之字形的纹理背景 但是 标题的垂直尺寸可能会改变 并且我无法将标题实
  • 单击按钮时滚动视图向上和向下滚动[重复]

    这个问题在这里已经有答案了 可能的重复 以编程方式滚动 UIScrollView https stackoverflow com questions 2234875 programmatically scroll a uiscrollvie
  • Mac Swampy(Python学习模块)安装

    我想教我的孩子们编程 并且正在使用 Downey 的 Think Python 这本书很棒 除了他安装 Swampy 一个类似海龟的学习模块时 我花了几个小时试图弄清楚 现在我需要帮助 任何人都可以提供有关如何安装 Swampy 的清晰 分
  • asp.net mvc 并检查用户是否登录

    我是 ASP NET MVC 新手 我需要检查用户是否在我的应用程序中登录 因此我将以下代码放在我的 global asax 中 void Application PreRequestHandlerExecute object sender
  • 无效写入——Valgrind

    您好 我在我的 c 程序中遇到了 munmap chunk invalid point 错误 主要问题是 我什至不确定指针可能变得无效的所有方式是什么 我已经检查了我的代码中是否有没有足够空间调用的字符串 但没有发现任何看起来会超出范围的情