Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

strncpy vs. strcpy

Name: Anonymous 2008-08-23 18:46

Both functions are perfectly fine to use as long as they're used correctly, with valid input, in a legible manner.

However, which one would you guys say is easier to misuse? Which one has a greater potential impact when misused in the most typical way? And which one do you see misused more often in real code?

Name: Anonymous 2008-08-23 18:54

What? Serious discussion? GTFO back to /pr

Okay, strncpy is safer than strcpy, but only a tad because it doesn't guarantee a zero-terminated string in destination.

Both are terrible functions from a terrible standard library (at least to today's standards). And zero-terminated strings suck.

Name: Anonymous 2008-08-23 19:33

Both suck. strlcpy.

Name: Anonymous 2008-08-23 19:36

>>3
Enjoy your unportable code.

And speaking of which, everyone should use strcpy_s instead! The 's' is for 'safe', so it has to be safe, right? Right? They wouldn't lie to us would they?

Name: Anonymous 2008-08-23 19:39

>>3
Masturbating monkeys.

Name: Anonymous 2008-08-23 19:48

>>4
I will, along with my Freedom.

Name: Anonymous 2008-08-23 19:56

>>6
Let's just get this much out of the way first: Richard M. Stallman is a dirty penniless motherfucking hippie.

Name: Anonymous 2008-08-23 20:05

Either function is only safe if you to check your input, so the minimal safety check in strncpy usually isn't worth the performance hit (padding dest with nulls = more mem I/O). Your new C code is performance-critical, right?

Name: Anonymous 2008-08-23 20:08

>>7
Who sleeps on the Abelson's couch. What have you accomplished that's better than that?

Name: Anonymous 2008-08-23 20:58

>>9

Name: Anonymous 2008-08-23 21:06

$ # The Quine programming language is optimized for the creation of quines.
$ cat test.qne
#!/usr/bin/quine
Hello World!
$ ./test.qne
#!/usr/bin/quine
Hello World!
$ ls -l /usr/bin/quine
lrwxr-xr-x 1 anon anon 8 2008-08-23 21:02 /usr/bin/quine -> /bin/cat
$

Name: Anonymous 2008-08-23 21:27

>>11
quines do not take any input

Name: Anonymous 2008-08-23 21:52

>>12
And?  Neither does test.qne.

Name: Anonymous 2008-08-23 22:30

I program C in my day job and I basically never use strncpy or strcpy. I basically never use any of the string functions, and instead just use functions that are more general: memcpy, memcmp, etc, and pad the strings myself.

Name: Anonymous 2008-08-23 22:58

>>8
void foo(char *str) {
  char buf[1024];
  buf[1023] = '\0';
  size_t len = strlen(str) + 1;
  strncpy(buf, str, len > 1023 ? 1023 : len); }
Now tell me, it might be an awkward usage of strncpy, but how exactly is this code not safe? Worst that could happen is that strlen(str) accesses virtual memory that hasn't been assigned to any block, usually meaning a segmentation fault.

Name: Anonymous 2008-08-23 23:01

>>14
Probably the easiest way to go. From now on I think I'm going to use snprintf() and just optimize for string-copying later if I really fucking need it.

Name: Anonymous 2008-08-23 23:30

strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l

Copy characters of one string to another. These are versions of strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l with security enhancements as described in Security Enhancements in the CRT.

errno_t strncpy_s(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count
);


lol microsoft

Name: Anonymous 2008-08-23 23:32

>>13
You should have found out that it does when you were writing your anonix kernel. If you were writing.

Name: >>13 2008-08-23 23:39

>>18
Why would I write anything for Anonix?  And no, it doesn't.

Name: Anonymous 2008-08-23 23:51

>>19
I guess you're right. All interpreters read files. Saying that you can't write quine in cat is same as saying you can't write quine in perl/python/other interpreters.
Oh and sorry about anonix, I thought you were one of them because of your name and group

Name: Anonymous 2008-08-24 0:04

cat(1) isn't an interpreter

Name: Anonymous 2008-08-24 0:12

>>21
Yeah, well, you know, that's just, like, your opinion, man.

Name: Anonymous 2008-08-24 0:13

>>17
Microsoft version:

CopyMultipleStringsSafeEx(lpcszDst, (sMultipleStringsDescriptor *) smsdStringList, ssaSecurityAttributes, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

Name: Anonymous 2008-08-24 0:39

>>23
I think I remember you from /pr/. You posted in some thread about the win32 API and made that joke about VB function calls. Either that, or this is a meme that I haven't recognized yet.

Name: Anonymous 2008-08-24 0:41

>>21
Sure it is.  Every byte is an instruction to print that byte to stdout.

Name: Anonymous 2008-08-24 1:36

It's an old meme

Name: Anonymous 2008-08-24 11:37

>>24
I visited /pr to introduce SICP, Satori and other important cultural elements there, but this accurate recreation of the Win32 API was much older here.

Actually, you wouldn't believe it, but I've invented this meme.

Name: Anonymous 2008-08-24 11:44

>>27
I'm afraid you copied this meme from me, actually; I should know, since I invented it.

Name: EY MEME FAN 2008-08-24 13:59

>>28
E'm efyeed yee cepeed yhey meme fyem me, ecyeeyyy; E yheeyd kyew, yeyce E eyveyyed ey.

Name: Anonymous 2010-12-17 1:21

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Anonymous 2011-02-04 17:47

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List