Skip to content

Commit b93600b

Browse files
Replace strcpy() and strcat() functions in ldlogger
1 parent f95c4c1 commit b93600b

File tree

5 files changed

+106
-39
lines changed

5 files changed

+106
-39
lines changed

analyzer/tools/build-logger/src/ldlogger-tool-gcc.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ static void getDefaultArguments(const char* prog_, LoggerVector* args_)
8383
ssize_t readSize;
8484
int incStarted = 0;
8585

86-
strcpy(command, prog_);
86+
safe_strcpy(command, prog_, PATH_MAX);
87+
8788
/* WARNING: this always gets the C++ compiler include
8889
* dirs even if we are compiling C file.
8990
* */
90-
strcat(command, " -xc++ -E -v - < /dev/null 2>&1");
91+
safe_strcat(command, " -xc++ -E -v - < /dev/null 2>&1", PATH_MAX);
9192

9293
cmdOut = popen(command, "r");
9394
if (!cmdOut)
@@ -215,9 +216,9 @@ char* findFullPath(const char* executable, char* fullpath) {
215216
char* dir;
216217
path = strdup(getenv("PATH"));
217218
for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":")) {
218-
strcpy(fullpath, dir);
219-
strcpy(fullpath + strlen(dir), "/");
220-
strcpy(fullpath + strlen(dir) + 1, executable);
219+
safe_strcpy(fullpath, dir, PATH_MAX);
220+
safe_strcat(fullpath, "/", PATH_MAX);
221+
safe_strcat(fullpath, executable, PATH_MAX);
221222
if (access(fullpath, F_OK ) != -1 ) {
222223
free(path);
223224
return fullpath;
@@ -300,17 +301,18 @@ void transformSomePathsAbsolute(LoggerVector* args_)
300301
const char* path = (const char*)args_->data[i] + strlen(*flag);
301302
if (*path)
302303
{
303-
char newPath[PATH_MAX];
304-
strcpy(newPath, *flag);
304+
// Increased buffer size for flags
305+
char newPath[PATH_MAX + 256];
306+
safe_strcpy(newPath, *flag, 256);
305307

306308
int hasEqual = *path == '=';
307309
if (hasEqual)
308310
{
309-
strcat(newPath, "=");
311+
safe_strcat(newPath, "=", 256);
310312
++path;
311313
}
312314

313-
loggerMakePathAbs(path, newPath + strlen(*flag) + hasEqual, 0);
315+
loggerMakePathAbs(path, newPath + strlen(newPath), 0);
314316
loggerVectorReplace(args_, i, loggerStrDup(newPath));
315317
}
316318
else
@@ -441,7 +443,7 @@ int loggerGccParserCollectActions(
441443
}
442444
else
443445
{
444-
strcpy(newPath, current);
446+
safe_strcpy(newPath, current, PATH_MAX);
445447
}
446448
loggerVectorAddUnique(&action->sources, loggerStrDup(newPath),
447449
(LoggerCmpFuc) &strcmp);

analyzer/tools/build-logger/src/ldlogger-tool-javac.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void readArgumentsFromFile(const char* file_, LoggerVector* args_)
4747
char* line = NULL;
4848
size_t lineSize = 0;
4949
ssize_t readSize;
50-
50+
5151
FILE* file = fopen(file_, "r");
5252
if (!file)
5353
{
@@ -121,18 +121,18 @@ static int handleClassPath(
121121

122122
**resCp_ = 0;
123123
classPath = (char*) malloc((strlen(cp_) + 1) * sizeof(char));
124-
strcpy(classPath, cp_);
124+
safe_strcpy(classPath, cp_, strlen(cp_) + 1);
125125

126126
for (cpPart = strtok(classPath, ":"); cpPart; cpPart = strtok(NULL, ":"))
127127
{
128128
size_t i;
129129
char** words;
130130
wordexp_t we;
131-
131+
132132
memset(&we, 0, sizeof(wordexp_t));
133133
if (wordexp(cpPart, &we, WRDE_NOCMD | WRDE_UNDEF) != 0)
134134
{
135-
strcpy(*resCp_, cp_);
135+
safe_strcpy(*resCp_, cp_, PATH_MAX);
136136
free(classPath);
137137
return 0;
138138
}
@@ -155,7 +155,7 @@ static int handleClassPath(
155155
if (!newMem)
156156
{
157157
/* Out of memory */
158-
strcpy(*resCp_, cp_);
158+
safe_strcpy(*resCp_, cp_, PATH_MAX);
159159
free(classPath);
160160
wordfree(&we);
161161
return 0;
@@ -165,8 +165,8 @@ static int handleClassPath(
165165
*resCpSize_ = currSize * 2;
166166
}
167167

168-
strcat(*resCp_, path);
169-
strcat(*resCp_, ":");
168+
safe_strcat(*resCp_, path, PATH_MAX);
169+
safe_strcat(*resCp_, ":", PATH_MAX);
170170
}
171171

172172
wordfree(&we);
@@ -202,16 +202,16 @@ static void processArg(const char* arg_, ParserData* data_)
202202
return;
203203
}
204204

205-
strcpy(argToAdd, arg_);
205+
safe_strcpy(argToAdd, arg_, PATH_MAX);
206206

207207
if (data_->state == InClassDir)
208208
{
209209
if (!loggerMakePathAbs(arg_, data_->classdir, 0))
210210
{
211-
strcpy(data_->classdir, arg_);
211+
safe_strcpy(data_->classdir, arg_, PATH_MAX);
212212
}
213213

214-
strcpy(argToAdd, data_->classdir);
214+
safe_strcpy(argToAdd, data_->classdir, PATH_MAX);
215215
data_->state = Normal;
216216
}
217217
else if (data_->state == InClassPath)
@@ -287,7 +287,7 @@ int loggerJavacParserCollectActions(
287287
LoggerVector fargs;
288288

289289
loggerVectorInit(&fargs);
290-
290+
291291
readArgumentsFromFile(argv_[i] + 1, &fargs);
292292
for (j = 0; j < fargs.size; ++j)
293293
{
@@ -330,17 +330,17 @@ int loggerJavacParserCollectActions(
330330
if (data.classdir[0] != 0)
331331
{
332332
char* fname = loggerGetFileName(src, 1);
333-
strcpy(outputFile, data.classdir);
334-
strcat(outputFile, "/");
335-
strcat(outputFile, fname);
336-
strcat(outputFile, ".class");
333+
safe_strcpy(outputFile, data.classdir, PATH_MAX);
334+
safe_strcat(outputFile, "/", PATH_MAX);
335+
safe_strcat(outputFile, fname, PATH_MAX);
336+
safe_strcat(outputFile, ".class", PATH_MAX);
337337
free(fname);
338338
}
339339
else
340340
{
341341
char* path = loggerGetFilePathWithoutExt(src);
342-
strcpy(outputFile, path);
343-
strcat(outputFile, ".class");
342+
safe_strcpy(outputFile, path, PATH_MAX);
343+
safe_strcat(outputFile, ".class", PATH_MAX);
344344
free(path);
345345
}
346346

analyzer/tools/build-logger/src/ldlogger-tool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ LoggerFile* loggerFileInitFromPath(LoggerFile* file_, const char* path_)
129129
if (!loggerMakePathAbs(path_, file_->path, 0))
130130
{
131131
/* fallback to the given path */
132-
strcpy(file_->path, path_);
132+
safe_strcpy(file_->path, path_, PATH_MAX);
133133
}
134134

135135
return file_;

analyzer/tools/build-logger/src/ldlogger-util.c

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <stdlib.h>
1717
#include <stdint.h>
1818
#include <stdio.h>
19+
#include <string.h>
1920
#include <sys/file.h>
2021
#include <sys/stat.h>
2122
#include <time.h>
@@ -29,12 +30,12 @@ static char* makePathAbsRec(const char* path_, char* resolved_)
2930

3031
if (realpath(path_, pathBuff))
3132
{
32-
pathBuff[PATH_MAX - 1] = 0;
33-
return strcpy(resolved_, pathBuff);
33+
safe_strcpy(resolved_, pathBuff, PATH_MAX);
34+
return resolved_;
3435
}
3536
else
3637
{
37-
strcpy(pathBuff, path_);
38+
safe_strcpy(pathBuff, path_, PATH_MAX);
3839
}
3940

4041
/* cut off the last part */
@@ -54,8 +55,8 @@ static char* makePathAbsRec(const char* path_, char* resolved_)
5455
*slashPos = 0;
5556
if (makePathAbsRec(pathBuff, resolved_))
5657
{
57-
strcat(resolved_, "/");
58-
strcat(resolved_, child);
58+
safe_strcat(resolved_, "/", PATH_MAX);
59+
safe_strcat(resolved_, child, PATH_MAX);
5960
return resolved_;
6061
}
6162

@@ -248,8 +249,8 @@ char* loggerMakePathAbs(const char* path_, char* resolved_, int mustExist_)
248249
return NULL;
249250
}
250251

251-
strcat(newPath, "/");
252-
strcat(newPath, path_);
252+
safe_strcat(newPath, "/", PATH_MAX);
253+
safe_strcat(newPath, path_, PATH_MAX);
253254
return makePathAbsRec(newPath, resolved_);
254255
}
255256

@@ -590,7 +591,7 @@ int aquireLock(char const* logFile_)
590591
return -1;
591592
}
592593

593-
strcat(lockFilePath, ".lock");
594+
safe_strcat(lockFilePath, ".lock", PATH_MAX);
594595
lockFile = open(lockFilePath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
595596
if (lockFile == -1)
596597
{
@@ -706,3 +707,43 @@ void logPrint(char* logLevel_, char* fileName_, int line_, char *fmt_,...)
706707
fclose(stream);
707708
freeLock(lockFd);
708709
}
710+
711+
char* safe_strcpy(char* dest, const char* src, size_t buf_size)
712+
{
713+
if (buf_size == 0)
714+
{
715+
fprintf(stderr, "ERROR: safe_strcpy failed, buffer size invalid\n");
716+
exit(1);
717+
}
718+
719+
strncpy(dest, src, buf_size);
720+
721+
if (dest[buf_size - 1] != '\0')
722+
{
723+
fprintf(stderr, "ERROR: safe_strcpy failed, string too long\n");
724+
exit(1);
725+
}
726+
727+
return dest;
728+
}
729+
730+
char* safe_strcat(char* dest, const char* src, size_t buf_size)
731+
{
732+
if (buf_size == 0)
733+
{
734+
fprintf(stderr, "ERROR: safe_strcat failed, buffer size invalid\n");
735+
exit(1);
736+
}
737+
738+
size_t n = buf_size - strlen(dest) - 1;
739+
740+
if (strlen(src) > n)
741+
{
742+
fprintf(stderr, "ERROR: safe_strcat failed, string too long\n");
743+
exit(1);
744+
}
745+
746+
strncat(dest, src, n);
747+
748+
return dest;
749+
}

analyzer/tools/build-logger/src/ldlogger-util.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int predictEscapedSize(const char* str_);
3838
* the documentation of predictEscapedSize() for some examples.
3939
*
4040
* The '\\', '\r' and '\v' characters are also escaped correctly.
41-
*
41+
*
4242
* @param str_ a string to escape (non null).
4343
* @param buff_ an output buffer (non null).
4444
* @return anways returns buff_.
@@ -85,7 +85,7 @@ typedef int (*LoggerPredFuc)(const void*);
8585
/**
8686
* A very simple vector.
8787
*/
88-
typedef struct _LoggerVector
88+
typedef struct _LoggerVector
8989
{
9090
/**
9191
* The actual size of the vector.
@@ -257,7 +257,7 @@ char* loggerGetFilePathWithoutExt(const char* absPath_);
257257

258258
/**
259259
* Return the file`s name with or without it`s extension.
260-
*
260+
*
261261
* @param absPath_ the absolute file path.
262262
* @param withoutExt_ with or without extension.
263263
* @return file name or NULL on error..
@@ -297,4 +297,28 @@ void freeLock(int lockFile_);
297297
*/
298298
void logPrint(char* logLevel_, char* fileName_, int line_, char* fmt_, ...);
299299

300+
/**
301+
* Safe version of strcpy that checks buffer size and exits
302+
* if string truncation would occur.
303+
*
304+
* @param dest destination buffer pointer.
305+
* @param src source buffer pointer.
306+
* @param buf_size size of the destination buffer.
307+
*
308+
* @return returns destination pointer dest.
309+
*/
310+
char* safe_strcpy(char* dest, const char* src, size_t buf_size);
311+
312+
/**
313+
* Safe version of strcat that checks buffer size and exits
314+
* if string truncation would occur.
315+
*
316+
* @param dest destination buffer pointer.
317+
* @param src source buffer pointer.
318+
* @param buf_size size of the destination buffer.
319+
*
320+
* @return returns destination pointer dest.
321+
*/
322+
char* safe_strcat(char* dest, const char* src, size_t buf_size);
323+
300324
#endif /* __LOGGER_UTIL_H__ */

0 commit comments

Comments
 (0)