Skip to content

Commit 7228c70

Browse files
authored
Merge pull request #4461 from markdryan/cpuid_riscv64_crash
Fix two issues with cpuid_riscv64.c
2 parents 3848d4e + e0b610d commit 7228c70

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

cpuid_riscv64.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ static char *cpuname[] = {
8484
"CPU_RISCV64_ZVL128B"
8585
};
8686

87+
static char *cpuname_lower[] = {
88+
"riscv64_generic",
89+
"c910v"
90+
};
91+
8792
int detect(void){
8893
#ifdef __linux
8994
FILE *infile;
@@ -92,23 +97,29 @@ int detect(void){
9297
char *pmodel = NULL, *pisa = NULL;
9398

9499
infile = fopen("/proc/cpuinfo", "r");
100+
if (!infile)
101+
return CPU_GENERIC;
95102
while (fgets(buffer, sizeof(buffer), infile)){
96103
if(!strncmp(buffer, "model name", 10)){
97104
strcpy(model_buffer, buffer);
98-
pmodel = strchr(isa_buffer, ':') + 1;
105+
pmodel = strchr(model_buffer, ':');
106+
if (pmodel)
107+
pmodel++;
99108
}
100109

101110
if(!strncmp(buffer, "isa", 3)){
102111
strcpy(isa_buffer, buffer);
103-
pisa = strchr(isa_buffer, '4') + 1;
112+
pisa = strchr(isa_buffer, '4');
113+
if (pisa)
114+
pisa++;
104115
}
105116
}
106117

107118
fclose(infile);
108119

109-
if (!pmodel)
120+
if (!pmodel || !pisa)
110121
return(CPU_GENERIC);
111-
122+
112123
if (strstr(pmodel, check_c910_str) && strchr(pisa, 'v'))
113124
return CPU_C910V;
114125

@@ -146,5 +157,5 @@ void get_cpuconfig(void){
146157
}
147158

148159
void get_libname(void){
149-
printf("riscv64\n");
160+
printf("%s", cpuname_lower[detect()]);
150161
}

0 commit comments

Comments
 (0)