Skip to content

Commit fde25e3

Browse files
authored
Merge pull request #881 from uyjulian/no_strncpy
Changes to avoid strncpy in EE and host code
2 parents 144b272 + a01d453 commit fde25e3

20 files changed

Lines changed: 72 additions & 109 deletions

File tree

ee/erl/src/erl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ struct erl_record_t * _init_load_erl_from_file(const char * fname, char * erl_id
10501050
argv[0] = erl_id;
10511051
argv[1] = 0;
10521052

1053-
snprintf(tfname, sizeof(tfname), "%s%s", _init_erl_prefix, fname);
1053+
snprintf(tfname, sizeof(tfname), "%*s%s", sizeof(_init_erl_prefix), _init_erl_prefix, fname);
10541054

10551055
return load_erl_from_file(tfname, 1, argv);
10561056
}
@@ -1093,7 +1093,7 @@ struct erl_record_t * _init_load_erl_from_file_to_addr(const char * fname, u32 a
10931093
argv[0] = erl_id;
10941094
argv[1] = 0;
10951095

1096-
snprintf(tfname, sizeof(tfname), "%s%s", _init_erl_prefix, fname);
1096+
snprintf(tfname, sizeof(tfname), "%*s%s", sizeof(_init_erl_prefix), _init_erl_prefix, fname);
10971097

10981098
return load_erl_from_file_to_addr(tfname, addr, 1, argv);
10991099
}

ee/font/src/fontx.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ int fontx_load_single_krom(fontx_t *fontx)
159159
fontx_header = (fontx_hdr*)fontx->font;
160160

161161
// define header as single-byte font
162-
strncpy(fontx_header->id, "FONTX2", 6);
162+
strcpy(fontx_header->id, "FONTX2");
163163
fontx_header->id[6] = '\0';
164-
strncpy(fontx_header->name, "KROM", 8);
164+
strcpy(fontx_header->name, "KROM");
165165
fontx_header->name[8] = '\0';
166166

167167
fontx_header->width = 8;
@@ -237,9 +237,9 @@ int fontx_load_double_krom(fontx_t *fontx)
237237
fontx_header = (fontx_hdr*)fontx->font;
238238

239239
// define the header as double-byte font
240-
strncpy(fontx_header->id, "FONTX2", 6);
240+
strcpy(fontx_header->id, "FONTX2");
241241
fontx_header->id[6] = '\0';
242-
strncpy(fontx_header->name, "KROM", 8);
242+
strcpy(fontx_header->name, "KROM");
243243
fontx_header->name[8] = '\0';
244244

245245
fontx_header->width = 16;

ee/ioprpgen/src/ioprpgen.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ ioprpgen_write_romdir_entry(const struct ioprpgen_ctx *ctx, const char *name, u3
7272
{
7373
struct ioprp_romdir_entry ent;
7474
memset(&ent, 0, sizeof(ent));
75-
if ( name )
76-
{
77-
strncpy(ent.m_name, name, sizeof(ent.m_name) - 1);
78-
ent.m_name[sizeof(ent.m_name) - 1] = 0;
79-
}
75+
strlcpy(ent.m_name, name ? name : "", sizeof(ent.m_name));
8076
ent.m_extinfo_size = extinfo_size;
8177
ent.m_data_size = data_size;
8278
return ctx->m_write_cb(ctx->m_write_cb_userdata, ctx, &ent, sizeof(ent)) == sizeof(ent);

ee/kernel/src/fileio.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ int fioOpen(const char *name, int mode)
176176
WaitSema(_fio_completion_sema);
177177

178178
arg.mode = mode;
179-
strncpy(arg.name, name, FIO_PATH_MAX - 1);
180-
arg.name[FIO_PATH_MAX - 1] = 0;
179+
strlcpy(arg.name, name, sizeof(arg.name));
181180

182181
if ((res = sceSifCallRpc(&_fio_cd, FIO_F_OPEN, _fio_block_mode, &arg, sizeof arg,
183182
_fio_recv_data, 4, (void *)_fio_intr, NULL)) >= 0) {
@@ -399,8 +398,7 @@ int fioRemove(const char *name)
399398
WaitSema(_fio_io_sema);
400399
WaitSema(_fio_completion_sema);
401400

402-
strncpy(arg.path, name, FIO_PATH_MAX - 1);
403-
arg.path[FIO_PATH_MAX - 1] = 0;
401+
strlcpy(arg.path, name, sizeof(arg.path));
404402

405403
if ((res = sceSifCallRpc(&_fio_cd, FIO_F_REMOVE, 0, &arg, sizeof arg,
406404
&arg, 4, (void *)_fio_intr, NULL)) >= 0) {
@@ -432,8 +430,7 @@ int fioMkdir(const char *path)
432430
WaitSema(_fio_io_sema);
433431
WaitSema(_fio_completion_sema);
434432

435-
strncpy(arg.path, path, FIO_PATH_MAX - 1);
436-
arg.path[FIO_PATH_MAX - 1] = 0;
433+
strlcpy(arg.path, path, sizeof(arg.path));
437434

438435
if ((res = sceSifCallRpc(&_fio_cd, FIO_F_MKDIR, 0, &arg, sizeof arg,
439436
&arg, 4, (void *)_fio_intr, NULL)) >= 0) {
@@ -465,8 +462,7 @@ int fioRmdir(const char *dirname)
465462
WaitSema(_fio_io_sema);
466463
WaitSema(_fio_completion_sema);
467464

468-
strncpy(arg.path, dirname, FIO_PATH_MAX - 1);
469-
arg.path[FIO_PATH_MAX - 1] = 0;
465+
strlcpy(arg.path, dirname, sizeof(arg.path));
470466

471467
if ((res = sceSifCallRpc(&_fio_cd, FIO_F_RMDIR, 0, &arg, sizeof arg,
472468
&arg, 4, (void *)_fio_intr, NULL)) >= 0) {
@@ -542,8 +538,7 @@ int fioDopen(const char *name)
542538
WaitSema(_fio_io_sema);
543539
WaitSema(_fio_completion_sema);
544540

545-
strncpy(arg.name, name, FIO_PATH_MAX - 1);
546-
arg.name[FIO_PATH_MAX - 1] = 0;
541+
strlcpy(arg.name, name, sizeof(arg.name));
547542

548543
if ((res = sceSifCallRpc(&_fio_cd, FIO_F_DOPEN, 0, &arg, sizeof arg,
549544
&arg, 4, (void *)_fio_intr, NULL)) >= 0) {
@@ -636,8 +631,7 @@ int fioGetstat(const char *name, io_stat_t *buf)
636631
WaitSema(_fio_completion_sema);
637632

638633
arg.p.buf = buf;
639-
strncpy(arg.name, name, FIO_PATH_MAX - 1);
640-
arg.name[FIO_PATH_MAX - 1] = 0;
634+
strlcpy(arg.name, name, sizeof(arg.name));
641635

642636
if (!IS_UNCACHED_SEG(buf))
643637
sceSifWriteBackDCache(buf, sizeof(io_stat_t));
@@ -670,8 +664,7 @@ int fioChstat(const char *name, io_stat_t *buf, u32 cbit)
670664

671665
arg.p.cbit = cbit;
672666
memcpy(&arg.stat, buf, sizeof(io_stat_t));
673-
strncpy(arg.name, name, FIO_PATH_MAX - 1);
674-
arg.name[FIO_PATH_MAX - 1] = 0;
667+
strlcpy(arg.name, name, sizeof(arg.name));
675668

676669
if ((res = sceSifCallRpc(&_fio_cd, FIO_F_CHSTAT, 0, &arg, sizeof arg,
677670
&arg, 4, (void *)_fio_intr, NULL)) >= 0) {
@@ -703,8 +696,7 @@ int fioFormat(const char *name)
703696
WaitSema(_fio_io_sema);
704697
WaitSema(_fio_completion_sema);
705698

706-
strncpy(arg.path, name, FIO_PATH_MAX - 1);
707-
arg.path[FIO_PATH_MAX - 1] = 0;
699+
strlcpy(arg.path, name, sizeof(arg.path));
708700

709701
if ((res = sceSifCallRpc(&_fio_cd, FIO_F_FORMAT, 0, &arg, sizeof arg,
710702
&arg, 4, (void *)_fio_intr, NULL)) >= 0) {

ee/kernel/src/iopheap.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ int SifLoadIopHeap(const char *path, void *addr)
104104
return -E_LIB_API_INIT;
105105

106106
arg.p.addr = addr;
107-
strncpy(arg.path, path, LIH_PATH_MAX - 1);
108-
arg.path[LIH_PATH_MAX - 1] = 0;
107+
strlcpy(arg.path, path, sizeof(arg.path));
109108

110109
if (sceSifCallRpc(&_ih_cd, 3, 0, &arg, sizeof arg, &arg, 4, NULL, NULL) < 0)
111110
return -E_SIF_RPC_CALL;

ee/kernel/src/loadfile.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ int _SifLoadModule(const char *path, int arg_len, const char *args, int *modres,
7575

7676
memset(&arg, 0, sizeof arg);
7777

78-
strncpy(arg.path, path, LF_PATH_MAX - 1);
79-
arg.path[LF_PATH_MAX - 1] = 0;
78+
strlcpy(arg.path, path, sizeof(arg.path));
8079

8180
if (args && arg_len) {
8281
arg.p.arg_len = arg_len > LF_ARG_MAX ? LF_ARG_MAX : arg_len;
@@ -167,8 +166,7 @@ int SifSearchModuleByName(const char *name)
167166
if (SifLoadFileInit() < 0)
168167
return -SCE_EBINDMISS;
169168

170-
strncpy(arg.name, name, LF_PATH_MAX - 1);
171-
arg.name[LF_PATH_MAX - 1] = 0;
169+
strlcpy(arg.name, name, sizeof(arg.name));
172170

173171
if (sceSifCallRpc(&_lf_cd, LF_F_SEARCH_MOD_BY_NAME, 0, &arg, sizeof arg, &arg, 4, NULL, NULL) < 0)
174172
return -SCE_ECALLMISS;
@@ -201,10 +199,8 @@ int _SifLoadElfPart(const char *path, const char *secname, t_ExecData *data, int
201199
if (SifLoadFileInit() < 0)
202200
return -SCE_EBINDMISS;
203201

204-
strncpy(arg.path, path, LF_PATH_MAX - 1);
205-
strncpy(arg.secname, secname, LF_ARG_MAX - 1);
206-
arg.path[LF_PATH_MAX - 1] = 0;
207-
arg.secname[LF_ARG_MAX - 1] = 0;
202+
strlcpy(arg.path, path, sizeof(arg.path));
203+
strlcpy(arg.secname, secname, sizeof(arg.secname));
208204

209205
if (sceSifCallRpc(&_lf_cd, fno, 0, &arg, sizeof arg, &arg,
210206
sizeof(t_ExecData), NULL, NULL) < 0)

ee/libcglue/src/glue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct passwd __dummy_passwd;
6161
__attribute__((constructor))
6262
static void __dummy_passwd_init(void)
6363
{
64-
strncpy(__dummy_passwd_loginbuf, "ps2user", sizeof(__dummy_passwd_loginbuf));
64+
strcpy(__dummy_passwd_loginbuf, "ps2user");
6565
__dummy_passwd.pw_name = &__dummy_passwd_loginbuf[0];
6666
__dummy_passwd.pw_passwd = "xxx";
6767
__dummy_passwd.pw_uid = 1000;

ee/libcglue/src/ps2sdkapi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ int __fioDreadHelper(void *userdata, struct dirent *dir)
280280
}
281281

282282
dir->d_fileno = rv; // TODO: This number should be in theory a unique number per file
283-
strncpy(dir->d_name, iodir.name, __MAXNAMLEN);
284-
dir->d_name[__MAXNAMLEN - 1] = 0;
283+
snprintf(dir->d_name, sizeof(dir->d_name), "%*s", (int)(sizeof(iodir.name) - 1), iodir.name);
285284
dir->d_reclen = 0;
286285
switch (iodir.stat.mode & FIO_SO_IFMT) {
287286
case FIO_SO_IFLNK: dir->d_type = DT_LNK; break;

ee/network/netman/src/rpc_client.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <kernel.h>
33
#include <sifrpc.h>
44
#include <string.h>
5+
#include <stdio.h>
56
#include <malloc.h>
67
#include <netman.h>
78
#include <netman_rpc.h>
@@ -293,7 +294,7 @@ int NetManSetMainIF(const char *name)
293294

294295
WaitSema(NetManIOSemaID);
295296

296-
strncpy(TransmitBuffer.netifName, name, NETMAN_NETIF_NAME_MAX_LEN);
297+
strlcpy(TransmitBuffer.netifName, name, sizeof(TransmitBuffer.netifName));
297298
TransmitBuffer.netifName[NETMAN_NETIF_NAME_MAX_LEN-1] = '\0';
298299
if((result=sceSifCallRpc(&NETMAN_rpc_cd, NETMAN_IOP_RPC_FUNC_SET_MAIN_NETIF, 0, &TransmitBuffer, NETMAN_NETIF_NAME_MAX_LEN, &ReceiveBuffer, sizeof(s32), NULL, NULL))>=0)
299300
result=ReceiveBuffer.result;
@@ -316,7 +317,7 @@ int NetManQueryMainIF(char *name)
316317
{
317318
if((result=ReceiveBuffer.QueryMainNetIFResult.result) == 0)
318319
{
319-
strncpy(name, ReceiveBuffer.QueryMainNetIFResult.name, NETMAN_NETIF_NAME_MAX_LEN);
320+
sprintf(name, "%*s", (int)sizeof(ReceiveBuffer.QueryMainNetIFResult.name), ReceiveBuffer.QueryMainNetIFResult.name);
320321
name[NETMAN_NETIF_NAME_MAX_LEN-1] = '\0';
321322
}
322323
}

ee/rpc/cdvd/src/libcdvd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ s32 sceCdSearchFile(sceCdlFILE *file, const char *name)
214214
}
215215
}
216216

217-
strncpy(searchFileSendBuff.name, name, 255);
218-
searchFileSendBuff.name[255] = '\0';
217+
strlcpy(searchFileSendBuff.name, name, sizeof(searchFileSendBuff.name));
219218
searchFileSendBuff.dest = &searchFileSendBuff;
220219

221220
if (CdDebug > 0)

0 commit comments

Comments
 (0)