FFstrbuf is now always null terminated

This commit is contained in:
Linus Dierheimer
2021-11-17 10:59:31 +01:00
parent f0e623c3de
commit 7ced537e59
8 changed files with 53 additions and 29 deletions
+2 -2
View File
@@ -237,12 +237,12 @@ const FFTerminalShellResult* ffDetectTerminalShell(FFinstance* instance)
init = true;
ffStrbufInit(&result.shellProcessName);
ffStrbufInit(&result.shellExe);
ffStrbufInitA(&result.shellExe, 128);
result.shellExeName = result.shellExe.chars;
ffStrbufInit(&result.shellVersion);
ffStrbufInit(&result.terminalProcessName);
ffStrbufInit(&result.terminalExe);
ffStrbufInitA(&result.terminalExe, 128);
result.terminalExeName = result.terminalExe.chars;
ffStrbufInit(&result.userShellExe);
+14 -6
View File
@@ -221,7 +221,7 @@ void ffPrintAndAppendToCache(FFinstance* instance, const char* moduleName, uint8
for(uint32_t i = 0; i < numArgs; i++)
{
FFstrbuf buffer;
ffStrbufInit(&buffer);
ffStrbufInitA(&buffer, 64);
ffFormatAppendFormatArg(&buffer, &arguments[i]);
ffStrbufWriteTo(&buffer, cache->split);
ffStrbufDestroy(&buffer);
@@ -421,13 +421,21 @@ void ffWriteFileContent(const char* fileName, const FFstrbuf* content)
void ffAppendFDContent(int fd, FFstrbuf* buffer)
{
ssize_t readed;
while((readed = read(fd, buffer->chars + buffer->length, buffer->allocated - buffer->length)) == (buffer->allocated - buffer->length))
{
buffer->length += (uint32_t) readed;
ffStrbufEnsureCapacity(buffer, buffer->allocated * 2);
ssize_t readed = 0;
ffStrbufEnsureFree(buffer, 31); // 32 - 1 for the null terminator
uint32_t free = ffStrbufGetFree(buffer);
while(
(readed = read(fd, buffer->chars + buffer->length, free)) > 0 &&
(uint32_t) readed == free
) {
buffer->length += readed;
ffStrbufEnsureCapacity(buffer, (buffer->allocated * 2) - 1); // -1 for null terminator
free = ffStrbufGetFree(buffer);
}
// In case of failure, read returns -1. We don't want to substract the length of the buffer.
if(readed > 0)
buffer->length += (uint32_t) readed;
+2 -2
View File
@@ -383,7 +383,7 @@ static void parseConfigFile(FFinstance* instance, FFdata* data, FILE* file)
ssize_t read;
FFstrbuf line;
ffStrbufInitA(&line, 128); //The default structure line needs this size
ffStrbufInitA(&line, 256); //The default structure line needs this size
while ((read = getline(&lineStart, &len, file)) != -1)
{
@@ -497,7 +497,7 @@ static inline void optionParseString(const char* key, const char* value, FFstrbu
fprintf(stderr, "Error: usage: %s <str>\n", key);
exit(477);
}
ffStrbufEnsureCapacity(buffer, 64); //This is not needed, as ffStrbufSetS will resize capacity if needed, but giving a higher start should improve performance
ffStrbufEnsureFree(buffer, 63); //This is not needed, as ffStrbufSetS will resize capacity if needed, but giving a higher start should improve performance
ffStrbufSetS(buffer, value);
}
-2
View File
@@ -372,8 +372,6 @@ FFvariant ffSettingsGetGSettings(FFinstance* instance, const char* schemaName, c
FFvariant ffSettingsGet(FFinstance* instance, const char* dconfKey, const char* gsettingsSchemaName, const char* gsettingsPath, const char* gsettingsKey, FFvarianttype type);
FFvariant ffSettingsGetXFConf(FFinstance* instance, const char* channelName, const char* propertyName, FFvarianttype type);
uint32_t ffSettingsGetRpmPackageCount(FFinstance* instance);
#ifdef __ANDROID__
void ffSettingsGetAndroidProperty(const char* propName, FFstrbuf* result);
#endif
+2 -2
View File
@@ -47,10 +47,10 @@ void ffPrintCPU(FFinstance* instance)
}
FFstrbuf name;
ffStrbufInit(&name);
ffStrbufInitA(&name, 64);
FFstrbuf vendor;
ffStrbufInit(&vendor);
ffStrbufInitA(&vendor, 64);
FFstrbuf physicalCoresString;
ffStrbufInit(&physicalCoresString);
+1 -1
View File
@@ -37,7 +37,7 @@ void ffPrintFont(FFinstance* instance)
ffFontInitPango(&gtk4, gtk4Raw->chars);
FFstrbuf gtk;
ffStrbufInit(&gtk);
ffStrbufInitA(&gtk, 64);
ffGetGtkPretty(&gtk, &gtk2.pretty, &gtk3.pretty, &gtk4.pretty);
if(instance->config.fontFormat.length == 0)
+30 -14
View File
@@ -86,32 +86,48 @@ void ffStrbufInitVF(FFstrbuf* strbuf, const char* format, va_list arguments)
ffStrbufAppendVF(strbuf, format, arguments);
}
void ffStrbufEnsureCapacity(FFstrbuf* strbuf, uint32_t allocate)
static void setCapacity(FFstrbuf* strbuf, uint32_t capacity)
{
if(strbuf->allocated >= allocate)
if(strbuf->allocated == 0)
{
strbuf->chars = malloc(sizeof(char) * capacity);
strbuf->chars[0] = '\0';
}
else
strbuf->chars = realloc(strbuf->chars, sizeof(char) * capacity);
strbuf->allocated = capacity;
}
void ffStrbufEnsureCapacity(FFstrbuf* strbuf, uint32_t capacity)
{
if(strbuf->allocated > capacity)
return;
if(strbuf->allocated == 0)
strbuf->chars = malloc(sizeof(char) * allocate);
else
strbuf->chars = realloc(strbuf->chars, sizeof(char) * allocate);
strbuf->allocated = allocate;
setCapacity(strbuf, capacity + 1); // + 1 for the null byte
}
void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free)
{
if(free == 0)
if(ffStrbufGetFree(strbuf) >= free)
return;
uint32_t allocate = strbuf->allocated;
if(allocate < 2)
allocate = 2;
while((strbuf->length + free) > allocate)
while((strbuf->length + free + 1) > allocate) // + 1 for the null byte
allocate *= 2;
ffStrbufEnsureCapacity(strbuf, allocate);
setCapacity(strbuf, allocate);
}
uint32_t ffStrbufGetFree(const FFstrbuf* strbuf)
{
if(strbuf->allocated == 0)
return 0;
return strbuf->allocated - strbuf->length - 1; // - 1 for the null byte
}
void ffStrbufClear(FFstrbuf* strbuf)
@@ -276,13 +292,13 @@ void ffStrbufAppendVF(FFstrbuf* strbuf, const char* format, va_list arguments)
va_list localArguments;
va_copy(localArguments, arguments);
uint32_t written = (uint32_t) vsnprintf(strbuf->chars + strbuf->length, strbuf->allocated - strbuf->length, format, localArguments);
uint32_t written = (uint32_t) vsnprintf(strbuf->chars + strbuf->length, strbuf->allocated - strbuf->length - 1, format, localArguments); // -1 for the null byte
va_end(localArguments);
if(strbuf->length + written >= strbuf->allocated)
if(strbuf->length + written >= strbuf->allocated - 1) // -1 for the null byte
{
ffStrbufEnsureCapacity(strbuf, strbuf->allocated * 2);
setCapacity(strbuf, strbuf->allocated * 2);
ffStrbufAppendVF(strbuf, format, arguments); //Try again with larger buffer
}
else
+2
View File
@@ -34,6 +34,8 @@ void ffStrbufInitVF(FFstrbuf* strbuf, const char* format, va_list arguments);
void ffStrbufEnsureCapacity(FFstrbuf* strbuf, uint32_t allocate);
void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free);
uint32_t ffStrbufGetFree(const FFstrbuf* strbuf);
void ffStrbufClear(FFstrbuf* strbuf);
void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value);