当我调用这个函数时,我的代码总是在strcmp
中中断,并返回一个Segmentation Error
,没有提供更多的信息。
stop_t *getStop(char *name) {
node_t *current = stop_list_head;
stop_t *stop;
while (current != NULL) {
stop = current->stop;
if (stop != NULL) {
if (stop->name != NULL) {
if (strcmp(stop->name, name) == 0) {
return stop;
}
}
}
current = current->next;
}
return NULL;
}
当我插入一个printf("%s", stop->name);
时,它返回了相同的分段错误,但在printf
上。
我该怎么补救呢?
最小重现性示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH_STOP 50
typedef struct {
int routeCounter;
double latitude;
double longitude;
char name[MAX_LENGTH_STOP + 1];
} stop_t;
typedef struct node {
stop_t *stop;
struct node *next;
} node_t;
void printStopList();
node_t *stop_list_head = NULL;
int main() {
stop_t *stopPtr = NULL;
stop_t stop;
char name[MAX_LENGTH_STOP + 1];
/* Input Example: Praca de Espanha ; */
fgets(name, BUFSIZ, stdin);
stopPtr = getStop(name);
/* Create new stop if it doesn't exist already, else print error.*/
if (stopPtr == NULL) {
generateStop(name);
} else {
printf("<Error 01>: Stop already exists.\n");
}
}
/* Determines if the stop already exists based on the name */
stop_t *getStop(char *name) {
node_t *current = stop_list_head;
stop_t *stop;
while (current != NULL) {
stop = current->stop;
if (stop != NULL) {
if (stop->name != NULL) {
if (strcmp(stop->name, name) == 0) {
return stop;
}
}
}
current = current->next;
}
return NULL;
}
/* Generates a stop instance and adds it to the global linked list*/
void generateStop(char name[]) {
stop_t *stop = NULL;
stop = (stop_t *)malloc(sizeof(stop_t));
strcpy(stop->name, name);
stop->routeCounter = 0;
addStopToList(stop);
free(stop);
}
/* Adds created stops to a global linked list (stop_list_head)*/
void addStopToList(stop_t *stop) {
node_t *new_node = (node_t *)malloc(sizeof(node_t));
node_t *current;
new_node->stop = stop;
new_node->next = NULL;
if (stop_list_head == NULL) {
stop_list_head = new_node;
} else {
current = stop_list_head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
free(new_node);
}
1条答案
按热度按时间4ktjp1zp1#
存在多个问题:
strcmp
访问stop->name
时name
时,fgets
传递了一个无效的大小BUFSIZ
,该大小远大于MAX_LENGTH_STOP + 1
。这可能会导致缓冲区溢出fgets()
的返回值,如果从空文件重定向,会导致无效行为。fgets()
在name
数组的末尾留下尾随的换行符:它将被存储到停止列表中。name
参数比stop_t
结构中的name
字段长,则strcpy(stop->name, name);
可能会导致缓冲区溢出。以下是修改后的版本: