c++ - Two scenarios of strcmp -
i have write buggy server crash due off 1 error. my doubt why below code gives segmenation fault
char wbuff[5]; char abuff[5]= "12345"; strcpy(wbuff,abuff);
but below code doesn't
char buf[bufsize]; char wbuf[5]; n = read(connfd, buf, bufsize); // read input string client strcpy(wbuf,buf); // strlen(buf) greater 5
the first 1 copies non-null-terminated string (abuff) other string. walks off end of abuff until finds 0 byte, quite ways, , causes segfault. other 1 copies properly-terminated string (buf) undersized string wbuf, , stops copying after copies strlen(buf)+1 bytes. first more cause segfault, both errors.
Comments
Post a Comment