From 927c9665ce01fdade6a0846ad60a561320272440 Mon Sep 17 00:00:00 2001 From: victor Date: Thu, 12 Jan 2006 05:00:08 +0000 Subject: replace some strncpy with strlcpy, or just strcpy if src is const git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3267 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- mbbsd/admin.c | 20 +++++++++----------- mbbsd/announce.c | 3 +-- mbbsd/bbs.c | 11 +++++------ mbbsd/chat.c | 8 ++++---- mbbsd/edit.c | 19 +++++++------------ mbbsd/lovepaper.c | 2 +- mbbsd/mail.c | 21 +++++++++------------ mbbsd/menu.c | 2 +- mbbsd/read.c | 3 +-- mbbsd/record.c | 2 +- mbbsd/register.c | 4 ++-- mbbsd/talk.c | 15 ++++++--------- mbbsd/user.c | 11 +++++------ mbbsd/xyz.c | 3 +-- 14 files changed, 53 insertions(+), 71 deletions(-) (limited to 'mbbsd') diff --git a/mbbsd/admin.c b/mbbsd/admin.c index 3082cb51..fa285709 100644 --- a/mbbsd/admin.c +++ b/mbbsd/admin.c @@ -557,8 +557,7 @@ m_mod_board(char *bname) break; } while (1); - if (strlen(genbuf) >= 4) - strncpy(newbh.title, genbuf, 4); + strcpy(newbh.title, genbuf); newbh.title[4] = ' '; @@ -591,11 +590,11 @@ m_mod_board(char *bname) clrtobot(); } if (newbh.brdattr & BRD_GROUPBOARD) - strncpy(newbh.title + 5, "Σ", 2); + strcpy(newbh.title + 5, "Σ"); else if (newbh.brdattr & BRD_NOTRAN) - strncpy(newbh.title + 5, "◎", 2); + strcpy(newbh.title + 5, "◎"); else - strncpy(newbh.title + 5, "●", 2); + strcpy(newbh.title + 5, "●"); if (HasUserPerm(PERM_SYSOP|PERM_BOARD) && !(newbh.brdattr & BRD_HIDE)) { getdata_str(14, 0, "設定讀寫權限(Y/N)?", ans, sizeof(ans), LCECHO, "N"); @@ -854,7 +853,7 @@ m_newbrd(int whatclass, int recover) break; } while (1); - strncpy(newboard.title, genbuf, 4); + strcpy(newboard.title, genbuf); newboard.title[4] = ' '; @@ -888,11 +887,11 @@ m_newbrd(int whatclass, int recover) } if (newboard.brdattr & BRD_GROUPBOARD) - strncpy(newboard.title + 5, "Σ", 2); + strcpy(newboard.title + 5, "Σ"); else if (newboard.brdattr & BRD_NOTRAN) - strncpy(newboard.title + 5, "◎", 2); + strcpy(newboard.title + 5, "◎"); else - strncpy(newboard.title + 5, "●", 2); + strcpy(newboard.title + 5, "●"); newboard.level = 0; getdata(11, 0, "板主名單:", newboard.BM, sizeof(newboard.BM), DOECHO); @@ -1006,8 +1005,7 @@ auto_scan(char fdata[][STRLEN], char ans[]) ans[0] = '0'; return 1; } - strncpy(temp, fdata[1], 2); - temp[2] = '\0'; + strlcpy(temp, fdata[1], 3); /* 疊字 */ if (!strncmp(temp, &(fdata[1][2]), 2)) { diff --git a/mbbsd/announce.c b/mbbsd/announce.c index f294140b..81944e9e 100644 --- a/mbbsd/announce.c +++ b/mbbsd/announce.c @@ -673,8 +673,7 @@ a_pastetagpost(menu_t * pm, int mode) setbfile(buf, bh->brdname, fhdr.filename); if (dashf(buf)) { - strncpy(title + 3, fhdr.title, TTLEN - 3); - title[TTLEN] = '\0'; + strlcpy(title + 3, fhdr.title, sizeof(title) - 3); a_copyitem(buf, title, 0, 0); if (mode) { mode--; diff --git a/mbbsd/bbs.c b/mbbsd/bbs.c index 965eb0fd..6f32deae 100644 --- a/mbbsd/bbs.c +++ b/mbbsd/bbs.c @@ -734,7 +734,7 @@ do_general(int isbid) move(21,0); outs("種類:"); for(i=0; i<8 && bp->posttype[i*4]; i++) - strncpy(ctype[i],bp->posttype+4*i,4); + strlcpy(ctype[i],bp->posttype+4*i,5); if(i==0) i=8; for(aborted=0; abortedposttype + i * 4, 4); - genbuf[4] = 0; + strlcpy(genbuf, bp->posttype + i * 4, 5); sprintf(title, "%d.", i + 1); if( !getdata_buf(2, 11, title, genbuf, 5, DOECHO) ) break; @@ -1081,7 +1080,7 @@ b_posttype(int ent, const fileheader_t * fhdr, const char *direct) posttype_f |= (1<posttype_f = posttype_f; - strncpy(bp->posttype, posttype, 32); /* 這邊應該要防race condition */ + strlcpy(bp->posttype, posttype, sizeof(bp->posttype)); /* 這邊應該要防race condition */ substitute_record(fn_board, bp, sizeof(boardheader_t), currbid); return FULLUPDATE; @@ -1503,7 +1502,7 @@ read_post(int ent, fileheader_t * fhdr, const char *direct) STATINC(STAT_READPOST_OLD); } brc_addlist(fhdr->filename); - strncpy(currtitle, subject(fhdr->title), TTLEN); + strlcpy(currtitle, subject(fhdr->title), sizeof(currtitle)); switch(more_result) { @@ -2294,7 +2293,7 @@ recommend(int ent, fileheader_t * fhdr, const char *direct) #endif lastrecommend = now; lastrecommend_bid = currbid; - strncpy(lastrecommend_fname, fhdr->filename, FNLEN); + strlcpy(lastrecommend_fname, fhdr->filename, sizeof(lastrecommend_fname)); return FULLUPDATE; } diff --git a/mbbsd/chat.c b/mbbsd/chat.c index fc8507d1..9f468052 100644 --- a/mbbsd/chat.c +++ b/mbbsd/chat.c @@ -66,7 +66,7 @@ struct ChatBuf { }; static int -chat_recv(struct ChatBuf *cb, int fd, char *chatroom, char *chatid) +chat_recv(struct ChatBuf *cb, int fd, char *chatroom, char *chatid, size_t chatid_size) { int c, len; char *bptr; @@ -88,12 +88,12 @@ chat_recv(struct ChatBuf *cb, int fd, char *chatroom, char *chatid) chat_clear(NULL); break; case 'n': - strncpy(chatid, bptr + 2, 8); + strlcpy(chatid, bptr + 2, chatid_size); print_chatid(chatid); clrtoeol(); break; case 'r': - strncpy(chatroom, bptr + 2, IDLEN - 1); + strlcpy(chatroom, bptr + 2, IDLEN); break; case 't': move(0, 0); @@ -449,7 +449,7 @@ t_chat(void) printchatline("◆ 噹!郵差又來了..."); } if (ch == I_OTHERDATA) {/* incoming */ - if (chat_recv(&chatbuf, cfd, chatroom, chatid) == -1) { + if (chat_recv(&chatbuf, cfd, chatroom, chatid, 9) == -1) { chatting = chat_send(cfd, "/b"); break; } diff --git a/mbbsd/edit.c b/mbbsd/edit.c index cade65f7..4ad69daa 100644 --- a/mbbsd/edit.c +++ b/mbbsd/edit.c @@ -1448,7 +1448,7 @@ write_header(FILE * fp, char *mytitle) // FIXME unused strlcpy(postlog.board, currboard, sizeof(postlog.board)); if (!strncmp(ptr, str_reply, 4)) ptr += 4; - strncpy(postlog.title, ptr, 65); + strlcpy(postlog.title, ptr, sizeof(postlog.title)); postlog.date = now; postlog.number = 1; append_record(".post", (fileheader_t *) & postlog, sizeof(postlog)); @@ -1553,19 +1553,15 @@ browse_sigs: if (ifuseanony) fprintf(fp, "\n--\n※ 發信站: " BBSNAME "(" MYHOSTNAME ") \n◆ From: %s\n", "匿名天使的家"); - else { + else +#endif + { char temp[33]; - strncpy(temp, fromhost, 31); - temp[32] = '\0'; + strlcpy(temp, fromhost, sizeof(temp)); fprintf(fp, "\n--\n※ 發信站: " BBSNAME "(" MYHOSTNAME ") \n◆ From: %s\n", temp); } -#else - strncpy(temp, fromhost, 15); - fprintf(fp, "\n--\n※ 發信站: " BBSNAME "(" MYHOSTNAME - ") \n◆ From: %s\n", temp); -#endif #endif } @@ -2719,8 +2715,7 @@ vedit(char *fpath, int saveheader, int *islocal) currutmp->mode = EDITING; currutmp->destuid = currstat; - strncpy(mytitle, save_title, STRLEN-2); - mytitle[STRLEN-1] = 0; + strlcpy(mytitle, save_title, sizeof(mytitle)); #ifdef DBCSAWARE mbcs_mode = (cuser.uflag & DBCSAWARE_FLAG) ? 1 : 0; @@ -2876,7 +2871,7 @@ vedit(char *fpath, int saveheader, int *islocal) case Ctrl('X'): /* Save and exit */ tmp = write_file(fpath, saveheader, islocal, mytitle); if (tmp != KEEP_EDITING) { - strncpy(save_title, mytitle, STRLEN-2); + strlcpy(save_title, mytitle, sizeof(save_title)); save_title[STRLEN-1] = 0; currutmp->mode = mode0; currutmp->destuid = destuid0; diff --git a/mbbsd/lovepaper.c b/mbbsd/lovepaper.c index f9018464..8e9f5267 100644 --- a/mbbsd/lovepaper.c +++ b/mbbsd/lovepaper.c @@ -94,7 +94,7 @@ x_love(void) sethomepath(buf1, receiver); stampfile(buf1, &mhdr); Rename(path, buf1); - strncpy(mhdr.title, save_title, TTLEN); + strlcpy(mhdr.title, save_title, sizeof(mhdr.title)); strlcpy(mhdr.owner, cuser.userid, sizeof(mhdr.owner)); sethomedir(path, receiver); if (append_record(path, &mhdr, sizeof(mhdr)) == -1) diff --git a/mbbsd/mail.c b/mbbsd/mail.c index 3bf0dd47..6fd322e9 100644 --- a/mbbsd/mail.c +++ b/mbbsd/mail.c @@ -121,7 +121,7 @@ mail_id(const char *id, const char *title, const char *src, const char *owner) if (stampfile(dst, &mhdr)) return 0; strlcpy(mhdr.owner, owner, sizeof(mhdr.owner)); - strncpy(mhdr.title, title, TTLEN); + strlcpy(mhdr.title, title, sizeof(mhdr.title)); mhdr.filemode = 0; Copy(src, dst); @@ -284,7 +284,7 @@ do_hold_mail(const char *fpath, const char *receiver, const char *holder) strlcpy(mymail.owner, "[備.忘.錄]", sizeof(mymail.owner)); if (receiver) { snprintf(title, sizeof(title), "(%s) %s", receiver, save_title); - strncpy(mymail.title, title, TTLEN); + strlcpy(mymail.title, title, sizeof(mymail.title)); } else strlcpy(mymail.title, save_title, sizeof(mymail.title)); @@ -332,7 +332,7 @@ do_send(const char *userid, const char *title) } /* process title */ if (title) - strncpy(save_title, title, STRLEN-1); + strlcpy(save_title, title, sizeof(save_title)); else getdata(2, 0, "主題:", save_title, STRLEN - 20, DOECHO); @@ -381,7 +381,7 @@ do_send(const char *userid, const char *title) return -2; } /* why not make title here? */ - strncpy(mhdr.title, save_title, TTLEN); + strlcpy(mhdr.title, save_title, sizeof(mhdr.title)); clear(); sethomefile(fpath, userid, FN_OVERRIDES); i = belong(fpath, cuser.userid); @@ -963,8 +963,7 @@ maildoent(int num, fileheader_t * ent) mark = "R:"; } - strncpy(datepart, ent->date, sizeof(datepart)-1); - datepart[sizeof(datepart)-1] = 0; + strlcpy(datepart, ent->date, sizeof(datepart)); switch(showmail_mode) { @@ -1048,7 +1047,7 @@ mail_read(int ent, fileheader_t * fhdr, const char *direct) clear(); setdirpath(buf, direct, fhdr->filename); - strncpy(currtitle, subject(fhdr->title), TTLEN); + strlcpy(currtitle, subject(fhdr->title), sizeof(currtitle)); done = delete_it = replied = NA; while (!done) { int more_result = more(buf, YEA); @@ -1373,8 +1372,7 @@ mail_cite(int ent, fileheader_t * fhdr, const char *direct) setuserfile(fpath, fhdr->filename); strlcpy(title, "◇ ", sizeof(title)); - strncpy(title + 3, fhdr->title, TTLEN - 3); - title[TTLEN] = '\0'; + strlcpy(title + 3, fhdr->title, sizeof(title) - 3); a_copyitem(fpath, title, 0, 1); if (cuser.userlevel >= PERM_BM) { @@ -1412,8 +1410,7 @@ mail_save(int ent, fileheader_t * fhdr, const char *direct) if (HasUserPerm(PERM_MAILLIMIT)) { setuserfile(fpath, fhdr->filename); strlcpy(title, "◇ ", sizeof(title)); - strncpy(title + 3, fhdr->title, TTLEN - 3); - title[TTLEN] = '\0'; + strlcpy(title + 3, fhdr->title, sizeof(title) - 3); a_copyitem(fpath, title, fhdr->owner, 1); sethomeman(fpath, cuser.userid); a_menu(cuser.userid, fpath, 1, NULL); @@ -1639,7 +1636,7 @@ send_inner_mail(const char *fpath, const char *title, const char *receiver) mymail.filemode = FILE_READ; } else strlcpy(mymail.owner, cuser.userid, sizeof(mymail.owner)); - strncpy(mymail.title, title, TTLEN); + strlcpy(mymail.title, title, sizeof(mymail.title)); unlink(fname); Copy(fpath, fname); sethomedir(fname, rightid); diff --git a/mbbsd/menu.c b/mbbsd/menu.c index 519fbf49..99c2c8ba 100644 --- a/mbbsd/menu.c +++ b/mbbsd/menu.c @@ -53,7 +53,7 @@ showtitle(const char *title, const char *mid) { board_hidden_status = ((getbcache(bid)->brdattr & BRD_HIDE) && (getbcache(bid)->brdattr & BRD_POSTMASK)); - strncpy(lastboard, currboard, sizeof(lastboard)); + strlcpy(lastboard, currboard, sizeof(lastboard)); } } diff --git a/mbbsd/read.c b/mbbsd/read.c index d20a0676..d5ece956 100644 --- a/mbbsd/read.c +++ b/mbbsd/read.c @@ -384,8 +384,7 @@ mail_forward(const fileheader_t * fhdr, const char *direct, int mode) char buf[STRLEN]; char *p; - strncpy(buf, direct, sizeof(buf)-1); - buf[sizeof(buf)-1] = 0; + strlcpy(buf, direct, sizeof(buf)); if ((p = strrchr(buf, '/'))) *p = '\0'; switch (i = doforward(buf, fhdr, mode)) { diff --git a/mbbsd/record.c b/mbbsd/record.c index e765198d..b7ed123b 100644 --- a/mbbsd/record.c +++ b/mbbsd/record.c @@ -568,7 +568,7 @@ append_record_forward(char *fpath, fileheader_t * record, int size, const char * int n; for (n = strlen(fpath) - 1; fpath[n] != '/' && n > 0; n--); - strncpy(buf, fpath, n + 1); + strlcpy(buf, fpath, n + 1); if (n + sizeof(".forward") > sizeof(buf)) return -1; strcpy(buf + n + 1, ".forward"); diff --git a/mbbsd/register.c b/mbbsd/register.c index 1a35c7bc..ebfb3bf0 100644 --- a/mbbsd/register.c +++ b/mbbsd/register.c @@ -254,14 +254,14 @@ new_register(void) outs("密碼太簡單,易遭入侵,至少要 4 個字,請重新輸入\n"); continue; } - strncpy(newuser.passwd, passbuf, PASSLEN); + strlcpy(newuser.passwd, passbuf, PASSLEN); getdata(20, 0, "請檢查密碼:", passbuf, sizeof(passbuf), NOECHO); if (strncmp(passbuf, newuser.passwd, PASSLEN)) { outs("密碼輸入錯誤, 請重新輸入密碼.\n"); continue; } passbuf[8] = '\0'; - strncpy(newuser.passwd, genpasswd(passbuf), PASSLEN); + strlcpy(newuser.passwd, genpasswd(passbuf), PASSLEN); break; } newuser.version = PASSWD_VERSION; diff --git a/mbbsd/talk.c b/mbbsd/talk.c index 15165c07..2ed398a6 100644 --- a/mbbsd/talk.c +++ b/mbbsd/talk.c @@ -638,7 +638,7 @@ my_write2(void) break; if (my_write(tw->pid, msg, tw->userid, i, tw->uin)) - strncpy(tw->msg[5].last_call_in, t_last_write, + strlcpy(tw->msg[5].last_call_in, t_last_write, sizeof(tw->msg[5].last_call_in)); break; } @@ -1205,14 +1205,12 @@ do_talk_char(talkwin_t * twin, int ch, FILE *flog) case Ctrl('M'): case Ctrl('J'): line = big_picture + twin->curln; - strncpy(buf, (char *)line->data, line->len); - buf[line->len] = 0; + strlcpy(buf, (char *)line->data, line->len + 1); do_talk_nextline(twin); break; case Ctrl('P'): line = big_picture + twin->curln; - strncpy(buf, (char *)line->data, line->len); - buf[line->len] = 0; + strlcpy(buf, (char *)line->data, line->len + 1); if (twin->curln > twin->sline) { --(twin->curln); move(twin->curln, twin->curcol); @@ -1226,8 +1224,7 @@ do_talk_char(talkwin_t * twin, int ch, FILE *flog) break; case Ctrl('N'): line = big_picture + twin->curln; - strncpy(buf, (char *)line->data, line->len); - buf[line->len] = 0; + strlcpy(buf, (char *)line->data, line->len + 1); if (twin->curln < twin->eline) { ++(twin->curln); move(twin->curln, twin->curcol); @@ -2416,7 +2413,7 @@ userlist(void) snprintf(buf, sizeof(buf), "故鄉 [%s]:", currutmp->from); if (!getdata(1, 0, buf, currutmp->from, sizeof(currutmp->from), DOECHO)) - strncpy(currutmp->from, buf, 23); + strlcpy(currutmp->from, buf, sizeof(currutmp->from)); redrawall = redraw = 1; } break; @@ -2608,7 +2605,7 @@ userlist(void) } msg.pid = currpid; - strncpy(msg.userid, cuser.userid, sizeof(msg.userid)); + strlcpy(msg.userid, cuser.userid, sizeof(msg.userid)); snprintf(msg.last_call_in, sizeof(msg.last_call_in), "[廣播]%s", genbuf); for (i = 0; i < SHM->UTMPnumber; ++i) { diff --git a/mbbsd/user.c b/mbbsd/user.c index 3060b88d..6c7d36f8 100644 --- a/mbbsd/user.c +++ b/mbbsd/user.c @@ -491,8 +491,7 @@ getregcode(char *buf) int i; /* init seed with magic */ - strncpy(buf, REGCODE_MAGIC, 13); /* des keys are only 13 byte */ - buf[13] = 0; + strlcpy(buf, REGCODE_MAGIC, 14); /* des keys are only 13 byte */ /* scramble with user id */ for (i = 0; i < IDLEN && uid[i]; i++) @@ -919,7 +918,7 @@ uinfo_query(userec_t *u, int adminmode, int unum) fail++; break; } - strncpy(genbuf, buf, PASSLEN); + strlcpy(genbuf, buf, PASSLEN); getdata(i++, 0, "請檢查新密碼:", buf, PASSLEN, NOECHO); if (strncmp(buf, genbuf, PASSLEN)) { @@ -928,7 +927,7 @@ uinfo_query(userec_t *u, int adminmode, int unum) break; } buf[8] = '\0'; - strncpy(x.passwd, genpasswd(buf), PASSLEN); + strlcpy(x.passwd, genpasswd(buf), sizeof(x.passwd)); break; case '3': @@ -1399,7 +1398,7 @@ toregister(char *email, char *genbuf, char *phone, char *career, outs("但注意手動認證通常會花上數天的時間。\n"); } } - strncpy(cuser.email, email, sizeof(cuser.email)); + strlcpy(cuser.email, email, sizeof(cuser.email)); REGFORM2: if (strcasecmp(email, "x") == 0) { /* 手動認證 */ if ((fn = fopen(fn_register, "a"))) { @@ -1424,7 +1423,7 @@ toregister(char *email, char *genbuf, char *phone, char *career, #endif snprintf(genbuf, sizeof(genbuf), "%s:%s:", phone, career); - strncpy(cuser.justify, genbuf, REGLEN); + strlcpy(cuser.justify, genbuf, sizeof(cuser.justify)); sethomefile(buf, cuser.userid, "justify"); } email_justify(&cuser); diff --git a/mbbsd/xyz.c b/mbbsd/xyz.c index fb48c846..f5dae9df 100644 --- a/mbbsd/xyz.c +++ b/mbbsd/xyz.c @@ -200,8 +200,7 @@ note(void) } while (buf[0] == 'e'); demoney(-5); strcpy(myitem.userid, cuser.userid); - strncpy(myitem.nickname, cuser.nickname, 18); - myitem.nickname[18] = '\0'; + strlcpy(myitem.nickname, cuser.nickname, sizeof(myitem.nickname)); myitem.date = now; /* begin load file */ -- cgit v1.2.3