Skip to content

Commit 3ea0406

Browse files
committed
CPU (Linux): fix var content was unexpectedly modified
1 parent 6429c20 commit 3ea0406

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/detection/cpu/cpu_linux.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ static void detectArmName(FFstrbuf* cpuinfo, FFCPUResult* cpu, uint32_t implId)
196196
{
197197
// https://github.com/Dr-Noob/cpufetch/issues/213#issuecomment-1927782105
198198
ffStrbufSetStatic(&cpu->name, "Virtualized Apple Silicon");
199+
ffStrbufGetlineRestore(&line, &len, cpuinfo);
199200
return;
200201
}
201202
name = applePartId2name(partId);
@@ -251,7 +252,10 @@ static const char* parseCpuInfo(
251252
&& cpu->name.length > 0 // #1202 #1204
252253
#endif
253254
)
255+
{
256+
ffStrbufGetlineRestore(&line, &len, cpuinfo);
254257
break;
258+
}
255259

256260
(void)(
257261
// arm64 doesn't have "model name"; arm32 does have "model name" but its value is not useful.
@@ -520,6 +524,8 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu)
520524
cpu->coresPhysical = (uint16_t) ffStrbufToUInt(&physicalCoresBuffer, cpu->coresLogical);
521525
#if __x86_64__ || __i386__
522526
cpu->packages = getPackageCount(&cpuinfo);
527+
if (cpu->packages > 1)
528+
cpu->coresPhysical *= cpu->packages;
523529
#endif
524530

525531
// Ref https://github.com/fastfetch-cli/fastfetch/issues/1194#issuecomment-2295058252

src/util/FFstrbuf.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,22 @@ bool ffStrbufGetline(char** lineptr, size_t* n, FFstrbuf* buffer)
570570
return true;
571571
}
572572

573+
/// @brief Restore the end of a line that was modified by ffStrbufGetline.
574+
/// @warning This function should be called before breaking an ffStrbufGetline loop.
575+
void ffStrbufGetlineRestore(char** lineptr, size_t* n, FFstrbuf* buffer)
576+
{
577+
assert(buffer && lineptr && n);
578+
assert(buffer->allocated > 0 || (buffer->allocated == 0 && buffer->length == 0));
579+
assert(!*lineptr || (*lineptr >= buffer->chars && *lineptr <= buffer->chars + buffer->length));
580+
581+
if (!*lineptr)
582+
return;
583+
584+
*lineptr += *n;
585+
if (*lineptr < buffer->chars + buffer->length)
586+
**lineptr = '\n';
587+
}
588+
573589
bool ffStrbufRemoveDupWhitespaces(FFstrbuf* strbuf)
574590
{
575591
if (strbuf->allocated == 0) return false; // Doesn't work with static strings

src/util/FFstrbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void ffStrbufUpperCase(FFstrbuf* strbuf);
9090
void ffStrbufLowerCase(FFstrbuf* strbuf);
9191

9292
bool ffStrbufGetline(char** lineptr, size_t* n, FFstrbuf* buffer);
93+
void ffStrbufGetlineRestore(char** lineptr, size_t* n, FFstrbuf* buffer);
9394
bool ffStrbufRemoveDupWhitespaces(FFstrbuf* strbuf);
9495

9596
FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateA(uint32_t allocate)

0 commit comments

Comments
 (0)