Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use memcpy_s with ObjC on iOS?

Is there any replacement for memcpy in iOS? So far as I know, memcpy is not 'safe', and the suggested alternative is 'memcpy_s'

But, code fail to compile due to 'Undefined symbols for architecture armv7:' problem, after replacing memcpy with memcpy_s.

How can I fix this problem? How to setup the project settings? Any help will be appreciated.

Some code from AsyncSocket.m:

- (CFIndex)readIntoBuffer:(UInt8 *)buffer maxLength:(CFIndex)length
{
    if([_partialReadBuffer length] > 0)
    {
        // Determine the maximum amount of data to read
        CFIndex bytesToRead = MIN(length, [_partialReadBuffer length]);

        // Copy the bytes from the buffer
        memcpy(buffer, [_partialReadBuffer bytes], bytesToRead);

        // Remove the copied bytes from the buffer
        [_partialReadBuffer replaceBytesInRange:NSMakeRange(0, bytesToRead) withBytes:NULL length:0];

        return bytesToRead;
    }
    else
    {
        return CFReadStreamRead(_theReadStream, buffer, length);
    }
}
like image 643
Sanbrother Avatar asked Oct 18 '25 07:10

Sanbrother


1 Answers

as I know, memcpy is not 'safe'

That's not true as-is. In contrast with some really unsafe stdlib functions, it is only "not safe" if you can't use it. memcpy() takes a buffer length as its third argument, so you don't risk buffer overflows; you can also check for the source and target pointers in order to avoid dereferencing NULL, etc.

memcpy_s() is a Microsoft extension, and a such, it's only available on Windows (fortunately). If you need memcpy(), then use it, and don't try to replace standard functions with vendor-specific stuff (especially not if that vendor is Microsoft).


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!