Skip to content

Commit

Permalink
Merge pull request #3690 from b4n/vstring/type
Browse files Browse the repository at this point in the history
Improve vStringPut() usage safety and fix problematic callers.
  • Loading branch information
b4n committed May 24, 2023
2 parents bf1ba55 + 1128712 commit 979433d
Show file tree
Hide file tree
Showing 31 changed files with 115 additions and 92 deletions.
2 changes: 1 addition & 1 deletion dsl/optscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -4184,7 +4184,7 @@ op__put_str (OptVM *vm, EsObject *name,
for (size_t i = 0; i < d; i++)
vStringPut (vstr, ' ');
if (c != 0)
vStringPut (vstr, (char)c);
vStringPut (vstr, c);
}

ptrArrayDeleteLastInBatch (vm->ostack, 3);
Expand Down
5 changes: 2 additions & 3 deletions main/field.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,12 @@ extern bool doesFieldHaveTabOrNewlineChar (fieldType type, const tagEntryInfo *
static const char* renderCompactInputLine (vString *b, const char *const line)
{
bool lineStarted = false;
const char *p;
int c;

/* Write everything up to, but not including, the newline.
*/
for (p = line, c = *p ; c != NEWLINE && c != '\0' ; c = *++p)
for (const char *p = line; *p != NEWLINE && *p != '\0'; ++p)
{
int c = (unsigned char) *p;
if (lineStarted || ! isspace (c)) /* ignore leading spaces */
{
lineStarted = true;
Expand Down
8 changes: 4 additions & 4 deletions main/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ static void processExtraTagsOption (

longName = vStringNewOrClearWithAutoRelease (longName);

while ((c = *p++) != '\0')
while ((c = (unsigned char) *p++) != '\0')
{
switch (c)
{
Expand Down Expand Up @@ -1374,7 +1374,7 @@ static void processFieldsOption (
else if (*p != '+' && *p != '-')
resetFieldsOption (LANG_IGNORE, false);

while ((c = *p++) != '\0') switch (c)
while ((c = (unsigned char) *p++) != '\0') switch (c)
{
case '+':
if (inLongName)
Expand Down Expand Up @@ -3084,7 +3084,7 @@ static bool processLangSpecificFieldsOption (const char *const option,
error (WARNING, "Wrong per language field specification: %s", p);

longName = vStringNewOrClearWithAutoRelease (longName);
while ((c = *p++) != '\0')
while ((c = (unsigned char) *p++) != '\0')
{
switch (c)
{
Expand Down Expand Up @@ -3185,7 +3185,7 @@ static bool processLangSpecificExtraOption (const char *const option,
error (WARNING, "Wrong per language extra specification: %s", p);

longName = vStringNewOrClearWithAutoRelease (longName);
while ((c = *p++) != '\0')
while ((c = (unsigned char) *p++) != '\0')
{
switch (c)
{
Expand Down
12 changes: 6 additions & 6 deletions main/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ static vString* determineInterpreter (const char* const cmd)
for ( ; isspace ((int) *p) ; ++p)
; /* no-op */
for ( ; *p != '\0' && ! isspace ((int) *p) ; ++p)
vStringPut (interpreter, (int) *p);
vStringPut (interpreter, *p);
} while (strcmp (vStringValue (interpreter), "env") == 0);
return interpreter;
}
Expand Down Expand Up @@ -823,7 +823,7 @@ static vString* determineEmacsModeAtFirstLine (const char* const line)
for ( ; isspace ((int) *p) ; ++p)
; /* no-op */
for ( ; *p != '\0' && isLanguageNameChar ((int) *p) ; ++p)
vStringPut (mode, (int) *p);
vStringPut (mode, *p);

if ((strcmp(vStringValue (mode), "sh") == 0
|| strcmp(vStringValue (mode), "shell-script") == 0)
Expand All @@ -839,7 +839,7 @@ static vString* determineEmacsModeAtFirstLine (const char* const line)
goto out;

for ( ; p < end && isLanguageNameChar ((int) *p) ; ++p)
vStringPut (mode, (int) *p);
vStringPut (mode, *p);

for ( ; isspace ((int) *p) ; ++p)
; /* no-op */
Expand Down Expand Up @@ -891,7 +891,7 @@ static vString* determineEmacsModeAtEOF (MIO* const fp)
for ( ; isspace ((int) *p) ; ++p)
; /* no-op */
for ( ; *p != '\0' && isLanguageNameChar ((int) *p) ; ++p)
vStringPut (mode, (int) *p);
vStringPut (mode, *p);

is_shell_mode = ((strcasecmp (vStringValue (mode), "sh") == 0
|| strcasecmp (vStringValue (mode), "shell-script") == 0));
Expand Down Expand Up @@ -955,7 +955,7 @@ static vString* determineVimFileType (const char *const modeline)

p += strlen(filetype_prefix[i]);
for ( ; *p != '\0' && isalnum ((int) *p) ; ++p)
vStringPut (filetype, (int) *p);
vStringPut (filetype, *p);
break;
}
return filetype;
Expand Down Expand Up @@ -2542,7 +2542,7 @@ static void processLangKindDefinition (

longName = vStringNewOrClearWithAutoRelease (longName);

while ((c = *p++) != '\0')
while ((c = (unsigned char) *p++) != '\0')
{
switch (c)
{
Expand Down
10 changes: 3 additions & 7 deletions main/vstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,7 @@ extern void vStringCopyToLower (vString *const dest, const vString *const src)
vStringResize (dest, src->size);
d = dest->buffer;
for (i = 0 ; i < length ; ++i)
{
int c = s [i];

d [i] = tolower (c);
}
d [i] = (char) tolower (s [i]);
d [i] = '\0';
}

Expand Down Expand Up @@ -294,7 +290,7 @@ extern char *vStringStrdup (const vString *const string)
return str;
}

static char valueToXDigit (int v)
static int valueToXDigit (int v)
{
Assert (v >= 0 && v <= 0xF);

Expand All @@ -308,7 +304,7 @@ extern void vStringCatSWithEscaping (vString* b, const char *s)
{
for(; *s; s++)
{
int c = *s;
unsigned char c = (unsigned char) *s;

/* escape control characters (incl. \t) */
if ((c > 0x00 && c <= 0x1F) || c == 0x7F || c == '\\')
Expand Down
21 changes: 17 additions & 4 deletions main/vstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <stdio.h>

#include "debug.h"
#include "inline.h"

/*
Expand Down Expand Up @@ -101,18 +102,25 @@ CTAGS_INLINE void vStringPutNewlinAgainUnsafe (vString *const string)
string->buffer [string->length++] = '\n';
}

CTAGS_INLINE void vStringPut (vString *const string, const int c)
CTAGS_INLINE void vStringPutImpl (vString *const string, const int c)
{
/* verify the given character is an unsigned char value */
Assert (c >= 0 && c <= 0xff);

if (string->length + 1 == string->size) /* check for buffer overflow */
vStringResize (string, string->size * 2);

string->buffer [string->length] = c;
string->buffer [string->length] = (char) c;
if (c != '\0')
string->buffer [++string->length] = '\0';
}

CTAGS_INLINE bool vStringPutWithLimit (vString *const string, const int c,
unsigned int maxlen)
#define vStringPut(s, c) (sizeof(c) == sizeof(char) \
? vStringPutImpl((s), (unsigned char) (c)) \
: vStringPutImpl((s), (c)))

CTAGS_INLINE bool vStringPutWithLimitImpl (vString *const string, const int c,
unsigned int maxlen)
{
if (vStringLength (string) < maxlen || maxlen == 0)
{
Expand All @@ -122,4 +130,9 @@ CTAGS_INLINE bool vStringPutWithLimit (vString *const string, const int c,
return false;
}

#define vStringPutWithLimit(s, c, l) \
(sizeof(c) == sizeof(char) \
? vStringPutWithLimitImpl((s), (unsigned char) (c), (l)) \
: vStringPutWithLimitImpl((s), (c), (l)))

#endif /* CTAGS_MAIN_VSTRING_H */
2 changes: 1 addition & 1 deletion old-docs/EXTENDING.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ <h2>Examples</h2>
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, SwineKinds, K_DEFINE);
Expand Down
2 changes: 1 addition & 1 deletion parsers/abaqus.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static void createTag(AbaqusKind kind, const char *buf)

do
{
vStringPut(name, (int) *buf);
vStringPut(name, *buf);
++buf;
} while ((*buf != '\0') && (*buf != ','));
makeSimpleTag(name, kind);
Expand Down
22 changes: 11 additions & 11 deletions parsers/asp.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_FUNCTION);
Expand All @@ -140,7 +140,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_SUB);
Expand All @@ -149,7 +149,7 @@ static void findAspTags (void)
else {
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_DIM);
Expand All @@ -171,7 +171,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_FUNCTION);
Expand All @@ -184,7 +184,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_SUB);
Expand All @@ -193,7 +193,7 @@ static void findAspTags (void)
else {
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_DIM);
Expand All @@ -213,7 +213,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_FUNCTION);
Expand All @@ -231,7 +231,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_SUB);
Expand All @@ -249,7 +249,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_DIM);
Expand All @@ -267,7 +267,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_CLASS);
Expand All @@ -285,7 +285,7 @@ static void findAspTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
makeSimpleTag (name, K_CONST);
Expand Down
8 changes: 4 additions & 4 deletions parsers/autoit.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static int parseFunc (const unsigned char *p, NestingLevels *nls)
skipSpaces (&p);
while (isIdentChar ((int) *p))
{
vStringPut (name, (int) *p);
vStringPut (name, *p);
++p;
}
skipSpaces (&p);
Expand All @@ -154,7 +154,7 @@ static int parseFunc (const unsigned char *p, NestingLevels *nls)
vString *signature = vStringNew ();

do
vStringPut (signature, (int) *p);
vStringPut (signature, *p);
while (*p != ')' && *p++);

k = makeAutoItTag (nls, name, K_FUNCTION, signature);
Expand Down Expand Up @@ -218,7 +218,7 @@ static void findAutoItTags (void)
++p;
while (*p != '\0' && *p != '>' && *p != '"')
{
vStringPut (name, (int) *p);
vStringPut (name, *p);
++p;
}
if (vStringLength(name) > 0)
Expand Down Expand Up @@ -284,7 +284,7 @@ static void findAutoItTags (void)
p++;
while (isIdentChar ((int) *p))
{
vStringPut (name, (int) *p);
vStringPut (name, *p);
++p;
}
if (vStringLength(name) > 0)
Expand Down
2 changes: 1 addition & 1 deletion parsers/awk.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void findAwkTags (void)
++cp;
while (isalnum ((int) *cp) || *cp == '_')
{
vStringPut (name, (int) *cp);
vStringPut (name, *cp);
++cp;
}
while (isspace ((int) *cp))
Expand Down
17 changes: 16 additions & 1 deletion parsers/c-based.c
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,22 @@ static void skipToMatch (const char *const pair)
while (matchLevel > 0 && (c = skipToNonWhite ()) != EOF)
{
if (CollectingSignature)
vStringPut (Signature, c);
{
if (c <= 0xff)
vStringPut (Signature, c);
else
{
switch (c)
{
case CHAR_SYMBOL:
case STRING_SYMBOL:
vStringCat (Signature, cppGetLastCharOrStringContents ());
break;
default:
AssertNotReached();
}
}
}
if (c == begin)
{
++matchLevel;
Expand Down
2 changes: 1 addition & 1 deletion parsers/css.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void parseSelector (vString *const string, const int firstChar)
int c = firstChar;
do
{
vStringPut (string, (char) c);
vStringPut (string, c);
c = getcFromInputFile ();
} while (isSelectorChar (c));
ungetcToInputFile (c);
Expand Down
2 changes: 1 addition & 1 deletion parsers/erlang.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static const unsigned char *parseIdentifier (
vStringClear (identifier);
while (isIdentifierCharacter ((int) *cp))
{
vStringPut (identifier, (int) *cp);
vStringPut (identifier, *cp);
++cp;
}
return cp;
Expand Down
Loading

0 comments on commit 979433d

Please sign in to comment.