Skip to content

Commit 713cec3

Browse files
committed
Remove every instance of strcpy
Replacing it with snprintf or memcpy as appropriate.
1 parent 2896db6 commit 713cec3

File tree

7 files changed

+44
-40
lines changed

7 files changed

+44
-40
lines changed

contrib/minizip/miniunz.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static int makedir(const char *newdir) {
151151
printf("Error allocating memory\n");
152152
return UNZ_INTERNALERROR;
153153
}
154-
strcpy(buffer,newdir);
154+
memcpy(buffer, newdir, len + 1);
155155

156156
if (buffer[len-1] == '/') {
157157
buffer[len-1] = '\0';

contrib/minizip/zip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ extern int ZEXPORT zipAlreadyThere(zipFile file, char const *name) {
500500
/* Return true if name is in the central directory. */
501501
size_t len = strlen(name);
502502
char *copy = set_alloc(&zip->set, NULL, len + 1);
503-
strcpy(copy, name);
503+
memcpy(copy, name, len + 1);
504504
int found = set_found(&zip->set, copy);
505505
set_free(&zip->set, copy);
506506
return found;

contrib/untgz/untgz.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ char *TGZfname (const char *arcname)
133133
static char buffer[1024];
134134
int origlen,i;
135135

136-
strcpy(buffer,arcname);
137-
origlen = strlen(buffer);
136+
origlen = strlen(arcname);
137+
if (origlen >= sizeof(buffer))
138+
return NULL;
139+
memcpy(buffer, arcname, origlen + 1);
138140

139141
for (i=0; TGZsuffix[i]; i++)
140142
{
141-
strcpy(buffer+origlen,TGZsuffix[i]);
143+
snprintf(buffer + origlen, sizeof(buffer) - origlen, "%s", TGZsuffix[i]);
142144
if (access(buffer,F_OK) == 0)
143145
return buffer;
144146
}

examples/gzlog.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
#include <sys/types.h>
227227
#include <stdio.h> /* rename, fopen, fprintf, fclose */
228228
#include <stdlib.h> /* malloc, free */
229-
#include <string.h> /* strlen, strrchr, strcpy, strncpy, strcmp */
229+
#include <string.h> /* strlen, strrchr, strncpy, strcmp, memcpy */
230230
#include <fcntl.h> /* open */
231231
#include <unistd.h> /* lseek, read, write, close, unlink, sleep, */
232232
/* ftruncate, fsync */
@@ -350,7 +350,7 @@ local int log_lock(struct log *log)
350350
int fd;
351351
struct stat st;
352352

353-
strcpy(log->end, ".lock");
353+
memcpy(log->end, ".lock", sizeof(".lock"));
354354
while ((fd = open(log->path, O_CREAT | O_EXCL, 0644)) < 0) {
355355
if (errno != EEXIST)
356356
return -1;
@@ -373,7 +373,7 @@ local void log_touch(struct log *log)
373373
{
374374
struct stat st;
375375

376-
strcpy(log->end, ".lock");
376+
memcpy(log->end, ".lock", sizeof(".lock"));
377377
utimes(log->path, NULL);
378378
if (stat(log->path, &st) == 0)
379379
log->lock = st.st_mtime;
@@ -385,7 +385,7 @@ local int log_check(struct log *log)
385385
{
386386
struct stat st;
387387

388-
strcpy(log->end, ".lock");
388+
memcpy(log->end, ".lock", sizeof(".lock"));
389389
if (stat(log->path, &st) || st.st_mtime != log->lock)
390390
return 1;
391391
log_touch(log);
@@ -397,7 +397,7 @@ local void log_unlock(struct log *log)
397397
{
398398
if (log_check(log))
399399
return;
400-
strcpy(log->end, ".lock");
400+
memcpy(log->end, ".lock", sizeof(".lock"));
401401
unlink(log->path);
402402
log->lock = 0;
403403
}
@@ -556,7 +556,7 @@ local int log_append(struct log *log, unsigned char *data, size_t len)
556556
/* write the extra field, marking the log file as done, delete .add file */
557557
if (log_mark(log, NO_OP))
558558
return -1;
559-
strcpy(log->end, ".add");
559+
memcpy(log->end, ".add", sizeof(".add"));
560560
unlink(log->path); /* ignore error, since may not exist */
561561
return 0;
562562
}
@@ -574,17 +574,17 @@ local int log_replace(struct log *log)
574574
char *dest;
575575

576576
/* delete foo.add file */
577-
strcpy(log->end, ".add");
577+
memcpy(log->end, ".add", sizeof(".add"));
578578
unlink(log->path); /* ignore error, since may not exist */
579579
BAIL(3);
580580

581581
/* rename foo.name to foo.dict, replacing foo.dict if it exists */
582-
strcpy(log->end, ".dict");
582+
memcpy(log->end, ".dict", sizeof(".dict"));
583583
dest = malloc(strlen(log->path) + 1);
584584
if (dest == NULL)
585585
return -2;
586-
strcpy(dest, log->path);
587-
strcpy(log->end, ".temp");
586+
memcpy(dest, log->path, strlen(log->path) + 1);
587+
memcpy(log->end, ".temp", sizeof(".temp"));
588588
ret = rename(log->path, dest);
589589
free(dest);
590590
if (ret && errno != ENOENT)
@@ -625,7 +625,7 @@ local int log_compress(struct log *log, unsigned char *data, size_t len)
625625
return -2;
626626

627627
/* read in dictionary (last 32K of data that was compressed) */
628-
strcpy(log->end, ".dict");
628+
memcpy(log->end, ".dict", sizeof(".dict"));
629629
fd = open(log->path, O_RDONLY, 0);
630630
if (fd >= 0) {
631631
dict = read(fd, buf, DICT);
@@ -721,7 +721,7 @@ local void log_log(struct log *log, int op, char *record)
721721
FILE *rec;
722722

723723
now = time(NULL);
724-
strcpy(log->end, ".repairs");
724+
memcpy(log->end, ".repairs", sizeof(".repairs"));
725725
rec = fopen(log->path, "a");
726726
if (rec == NULL)
727727
return;
@@ -747,7 +747,7 @@ local int log_recover(struct log *log, int op)
747747

748748
/* load foo.add file if expected and present */
749749
if (op == APPEND_OP || op == COMPRESS_OP) {
750-
strcpy(log->end, ".add");
750+
memcpy(log->end, ".add", sizeof(".add"));
751751
if (stat(log->path, &st) == 0 && st.st_size) {
752752
len = (size_t)(st.st_size);
753753
if ((off_t)len != st.st_size ||
@@ -827,7 +827,7 @@ local int log_open(struct log *log)
827827
return -1;
828828

829829
/* open the log file, foo.gz */
830-
strcpy(log->end, ".gz");
830+
memcpy(log->end, ".gz", sizeof(".gz"));
831831
log->fd = open(log->path, O_RDWR | O_CREAT, 0644);
832832
if (log->fd < 0) {
833833
log_close(log);
@@ -842,7 +842,7 @@ local int log_open(struct log *log)
842842
log_close(log);
843843
return -1;
844844
}
845-
strcpy(log->end, ".dict");
845+
memcpy(log->end, ".dict", sizeof(".dict"));
846846
unlink(log->path);
847847
}
848848

@@ -877,7 +877,7 @@ gzlog *gzlog_open(char *path)
877877
log = malloc(sizeof(struct log));
878878
if (log == NULL)
879879
return NULL;
880-
strcpy(log->id, LOGID);
880+
memcpy(log->id, LOGID, sizeof(LOGID));
881881
log->fd = -1;
882882

883883
/* save path and end of path for name construction */
@@ -887,7 +887,7 @@ gzlog *gzlog_open(char *path)
887887
free(log);
888888
return NULL;
889889
}
890-
strcpy(log->path, path);
890+
memcpy(log->path, path, n + 1);
891891
log->end = log->path + n;
892892

893893
/* gain exclusive access and verify log file -- may perform a
@@ -951,7 +951,7 @@ int gzlog_compress(gzlog *logd)
951951
log_touch(log);
952952

953953
/* write the uncompressed data to the .add file */
954-
strcpy(log->end, ".add");
954+
memcpy(log->end, ".add", sizeof(".add"));
955955
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
956956
if (fd < 0)
957957
break;
@@ -961,7 +961,7 @@ int gzlog_compress(gzlog *logd)
961961
log_touch(log);
962962

963963
/* write the dictionary for the next compress to the .temp file */
964-
strcpy(log->end, ".temp");
964+
memcpy(log->end, ".temp", sizeof(".temp"));
965965
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
966966
if (fd < 0)
967967
break;
@@ -1012,7 +1012,7 @@ int gzlog_write(gzlog *logd, void *data, size_t len)
10121012
return -1;
10131013

10141014
/* create and write .add file */
1015-
strcpy(log->end, ".add");
1015+
memcpy(log->end, ".add", sizeof(".add"));
10161016
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
10171017
if (fd < 0)
10181018
return -1;
@@ -1055,7 +1055,7 @@ int gzlog_close(gzlog *logd)
10551055
/* free structure and return */
10561056
if (log->path != NULL)
10571057
free(log->path);
1058-
strcpy(log->id, "bad");
1058+
memcpy(log->id, "bad", sizeof("bad"));
10591059
free(log);
10601060
return 0;
10611061
}

gzlib.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ local gzFile gz_open(const void *path, int fd, const char *mode) {
221221
#if !defined(NO_snprintf) && !defined(NO_vsnprintf)
222222
(void)snprintf(state->path, len + 1, "%s", (const char *)path);
223223
#else
224-
strcpy(state->path, path);
224+
memcpy(state->path, path, len + 1);
225225
#endif
226226
}
227227

@@ -583,9 +583,13 @@ void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
583583
(void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
584584
"%s%s%s", state->path, ": ", msg);
585585
#else
586-
strcpy(state->msg, state->path);
587-
strcat(state->msg, ": ");
588-
strcat(state->msg, msg);
586+
{
587+
size_t path_len = strlen(state->path);
588+
size_t msg_len = strlen(msg);
589+
memcpy(state->msg, state->path, path_len);
590+
memcpy(state->msg + path_len, ": ", 2);
591+
memcpy(state->msg + path_len + 2, msg, msg_len + 1);
592+
}
589593
#endif
590594
}
591595

test/example.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void test_compress(Byte *compr, uLong comprLen, Byte *uncompr,
7171
err = compress(compr, &comprLen, (const Bytef*)hello, len);
7272
CHECK_ERR(err, "compress");
7373

74-
strcpy((char*)uncompr, "garbage");
74+
memcpy((char*)uncompr, "garbage", sizeof("garbage"));
7575

7676
err = uncompress(uncompr, &uncomprLen, compr, comprLen);
7777
CHECK_ERR(err, "uncompress");
@@ -118,7 +118,7 @@ static void test_gzio(const char *fname, Byte *uncompr, uLong uncomprLen) {
118118
fprintf(stderr, "gzopen error\n");
119119
exit(1);
120120
}
121-
strcpy((char*)uncompr, "garbage");
121+
memcpy((char*)uncompr, "garbage", sizeof("garbage"));
122122

123123
if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
124124
fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
@@ -209,7 +209,7 @@ static void test_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
209209
int err;
210210
z_stream d_stream; /* decompression stream */
211211

212-
strcpy((char*)uncompr, "garbage");
212+
memcpy((char*)uncompr, "garbage", sizeof("garbage"));
213213

214214
d_stream.zalloc = zalloc;
215215
d_stream.zfree = zfree;
@@ -301,7 +301,7 @@ static void test_large_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
301301
int err;
302302
z_stream d_stream; /* decompression stream */
303303

304-
strcpy((char*)uncompr, "garbage");
304+
memcpy((char*)uncompr, "garbage", sizeof("garbage"));
305305

306306
d_stream.zalloc = zalloc;
307307
d_stream.zfree = zfree;
@@ -375,7 +375,7 @@ static void test_sync(Byte *compr, uLong comprLen, Byte *uncompr,
375375
int err;
376376
z_stream d_stream; /* decompression stream */
377377

378-
strcpy((char*)uncompr, "garbage");
378+
memcpy((char*)uncompr, "garbage", sizeof("garbage"));
379379

380380
d_stream.zalloc = zalloc;
381381
d_stream.zfree = zfree;
@@ -450,7 +450,7 @@ static void test_dict_inflate(Byte *compr, uLong comprLen, Byte *uncompr,
450450
int err;
451451
z_stream d_stream; /* decompression stream */
452452

453-
strcpy((char*)uncompr, "garbage");
453+
memcpy((char*)uncompr, "garbage", sizeof("garbage"));
454454

455455
d_stream.zalloc = zalloc;
456456
d_stream.zfree = zfree;

test/infcover.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ local int try(char *hex, char *id, int err)
527527
assert(prefix != NULL);
528528

529529
/* first with inflate */
530-
strcpy(prefix, id);
531-
strcat(prefix, "-late");
530+
snprintf(prefix, strlen(id) + 6, "%s-late", id);
532531
mem_setup(&strm);
533532
strm.avail_in = 0;
534533
strm.next_in = Z_NULL;
@@ -553,8 +552,7 @@ local int try(char *hex, char *id, int err)
553552

554553
/* then with inflateBack */
555554
if (err >= 0) {
556-
strcpy(prefix, id);
557-
strcat(prefix, "-back");
555+
snprintf(prefix, strlen(id) + 6, "%s-back", id);
558556
mem_setup(&strm);
559557
ret = inflateBackInit(&strm, 15, win);
560558
assert(ret == Z_OK);

0 commit comments

Comments
 (0)