From a48a5f0cc6bfb750e5e971ce1e4388e90ea411bf Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 8 May 2025 19:42:57 +0200 Subject: [PATCH] Codechange: replace memcpy with std::copy_n --- src/3rdparty/md5/md5.cpp | 4 ++-- src/3rdparty/squirrel/squirrel/sqfuncstate.cpp | 2 +- src/3rdparty/squirrel/squirrel/sqstate.cpp | 2 +- src/blitter/32bpp_anim.cpp | 10 +++++----- src/blitter/32bpp_base.cpp | 8 ++++---- src/blitter/32bpp_optimized.cpp | 4 ++-- src/blitter/32bpp_sse2.cpp | 4 ++-- src/blitter/40bpp_anim.cpp | 10 +++++----- src/blitter/8bpp_base.cpp | 8 ++++---- src/blitter/8bpp_optimized.cpp | 2 +- src/network/core/address.cpp | 2 +- src/network/core/address.h | 4 ++-- src/network/core/host.cpp | 2 +- src/network/core/http_curl.cpp | 2 +- src/os/windows/crashlog_win.cpp | 3 +-- src/palette.cpp | 2 +- src/random_access_file.cpp | 2 +- src/safeguards.h | 3 +++ src/screenshot_bmp.cpp | 2 +- src/script/squirrel.cpp | 4 ++-- 20 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/3rdparty/md5/md5.cpp b/src/3rdparty/md5/md5.cpp index 0e58d989b6..d758f0e974 100644 --- a/src/3rdparty/md5/md5.cpp +++ b/src/3rdparty/md5/md5.cpp @@ -281,7 +281,7 @@ void Md5::Append(const void *data, const size_t nbytes) if (offset) { size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes); - memcpy(this->buf + offset, p, copy); + std::copy_n(p, copy, this->buf + offset); if (offset + copy < 64) return; @@ -294,7 +294,7 @@ void Md5::Append(const void *data, const size_t nbytes) for (; left >= 64; p += 64, left -= 64) this->Process(p); /* Process a final partial block. */ - if (left) memcpy(this->buf, p, left); + if (left) std::copy_n(p, left, this->buf); } void Md5::Finish(MD5Hash &digest) diff --git a/src/3rdparty/squirrel/squirrel/sqfuncstate.cpp b/src/3rdparty/squirrel/squirrel/sqfuncstate.cpp index 03fcf5f134..790805a3b5 100644 --- a/src/3rdparty/squirrel/squirrel/sqfuncstate.cpp +++ b/src/3rdparty/squirrel/squirrel/sqfuncstate.cpp @@ -534,7 +534,7 @@ SQFunctionProto *SQFuncState::BuildProto() for(SQUnsignedInteger no = 0; no < _lineinfos.size(); no++) f->_lineinfos[no] = _lineinfos[no]; for(SQUnsignedInteger no = 0; no < _defaultparams.size(); no++) f->_defaultparams[no] = _defaultparams[no]; - memcpy(f->_instructions,&_instructions[0],(size_t)_instructions.size()*sizeof(SQInstruction)); + std::copy_n(&_instructions[0], _instructions.size(), f->_instructions); f->_varparams = _varparams; diff --git a/src/3rdparty/squirrel/squirrel/sqstate.cpp b/src/3rdparty/squirrel/squirrel/sqstate.cpp index 42df3c5e71..f75c466da0 100644 --- a/src/3rdparty/squirrel/squirrel/sqstate.cpp +++ b/src/3rdparty/squirrel/squirrel/sqstate.cpp @@ -564,7 +564,7 @@ SQString *SQStringTable::Add(std::string_view new_string) SQString::SQString(std::string_view new_string) { - memcpy(_val,new_string.data(),new_string.size()); + std::ranges::copy(new_string, _val); _val[new_string.size()] = '\0'; _len = new_string.size(); _hash = string_table_hash(new_string); diff --git a/src/blitter/32bpp_anim.cpp b/src/blitter/32bpp_anim.cpp index 68275f001a..70869bd59a 100644 --- a/src/blitter/32bpp_anim.cpp +++ b/src/blitter/32bpp_anim.cpp @@ -389,11 +389,11 @@ void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, Colour *dst_pal = dst; uint16_t *anim_pal = anim_line; - memcpy(static_cast(dst), usrc, width * sizeof(uint32_t)); + std::copy_n(usrc, width, reinterpret_cast(dst)); usrc += width; dst += _screen.pitch; /* Copy back the anim-buffer */ - memcpy(anim_line, usrc, width * sizeof(uint16_t)); + std::copy_n(reinterpret_cast(usrc), width, anim_line); usrc = (const uint32_t *)&((const uint16_t *)usrc)[width]; anim_line += this->anim_buf_pitch; @@ -428,11 +428,11 @@ void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, in const uint16_t *anim_line = this->ScreenToAnimOffset((const uint32_t *)video) + this->anim_buf; for (; height > 0; height--) { - memcpy(udst, src, width * sizeof(uint32_t)); + std::copy_n(src, width, udst); src += _screen.pitch; udst += width; /* Copy the anim-buffer */ - memcpy(udst, anim_line, width * sizeof(uint16_t)); + std::copy_n(anim_line, width, reinterpret_cast(udst)); udst = (uint32_t *)&((uint16_t *)udst)[width]; anim_line += this->anim_buf_pitch; } @@ -459,7 +459,7 @@ void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &widt uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x); uint th = height - scroll_y; for (; th > 0; th--) { - memcpy(dst, src, tw * sizeof(uint16_t)); + std::copy_n(src, tw, dst); src -= this->anim_buf_pitch; dst -= this->anim_buf_pitch; } diff --git a/src/blitter/32bpp_base.cpp b/src/blitter/32bpp_base.cpp index 58acafc35a..2895d4feba 100644 --- a/src/blitter/32bpp_base.cpp +++ b/src/blitter/32bpp_base.cpp @@ -51,7 +51,7 @@ void Blitter_32bppBase::CopyFromBuffer(void *video, const void *src, int width, const uint32_t *usrc = (const uint32_t *)src; for (; height > 0; height--) { - memcpy(dst, usrc, width * sizeof(uint32_t)); + std::copy_n(usrc, width, dst); usrc += width; dst += _screen.pitch; } @@ -63,7 +63,7 @@ void Blitter_32bppBase::CopyToBuffer(const void *video, void *dst, int width, in const uint32_t *src = (const uint32_t *)video; for (; height > 0; height--) { - memcpy(udst, src, width * sizeof(uint32_t)); + std::copy_n(src, width, udst); src += _screen.pitch; udst += width; } @@ -75,7 +75,7 @@ void Blitter_32bppBase::CopyImageToBuffer(const void *video, void *dst, int widt const uint32_t *src = (const uint32_t *)video; for (; height > 0; height--) { - memcpy(udst, src, width * sizeof(uint32_t)); + std::copy_n(src, width, udst); src += _screen.pitch; udst += dst_pitch; } @@ -107,7 +107,7 @@ void Blitter_32bppBase::ScrollBuffer(void *video, int &left, int &top, int &widt } for (int h = height; h > 0; h--) { - memcpy(dst, src, width * sizeof(uint32_t)); + std::copy_n(src, width, dst); src -= _screen.pitch; dst -= _screen.pitch; } diff --git a/src/blitter/32bpp_optimized.cpp b/src/blitter/32bpp_optimized.cpp index e5df968205..7667597636 100644 --- a/src/blitter/32bpp_optimized.cpp +++ b/src/blitter/32bpp_optimized.cpp @@ -432,8 +432,8 @@ Sprite *Blitter_32bppOptimized::EncodeInternal(SpriteType sprite_type, const Spr dst->offset[1][z] = offset; offset += lengths[1][z]; - memcpy(dst->data + dst->offset[0][z], dst_px_orig[z].get(), lengths[0][z]); - memcpy(dst->data + dst->offset[1][z], dst_n_orig[z].get(), lengths[1][z]); + std::copy_n(reinterpret_cast(dst_px_orig[z].get()), lengths[0][z], dst->data + dst->offset[0][z]); + std::copy_n(reinterpret_cast(dst_n_orig[z].get()), lengths[1][z], dst->data + dst->offset[1][z]); } return dest_sprite; diff --git a/src/blitter/32bpp_sse2.cpp b/src/blitter/32bpp_sse2.cpp index 7d0b4c9edc..6fc08ca6cd 100644 --- a/src/blitter/32bpp_sse2.cpp +++ b/src/blitter/32bpp_sse2.cpp @@ -57,7 +57,7 @@ Sprite *Blitter_32bppSSE_Base::Encode(SpriteType sprite_type, const SpriteLoader dst_sprite->width = root_sprite.width; dst_sprite->x_offs = root_sprite.x_offs; dst_sprite->y_offs = root_sprite.y_offs; - memcpy(dst_sprite->data, &sd, sizeof(SpriteData)); + std::copy_n(reinterpret_cast(&sd), sizeof(SpriteData), dst_sprite->data); /* Copy colours and determine flags. */ bool has_remap = false; @@ -135,7 +135,7 @@ Sprite *Blitter_32bppSSE_Base::Encode(SpriteType sprite_type, const SpriteLoader if (has_translucency) sd.flags.Set(SpriteFlag::Translucent); if (!has_remap) sd.flags.Set(SpriteFlag::NoRemap); if (!has_anim) sd.flags.Set(SpriteFlag::NoAnim); - memcpy(dst_sprite->data, &sd, sizeof(SpriteData)); + std::copy_n(reinterpret_cast(&sd), sizeof(SpriteData), dst_sprite->data); return dst_sprite; } diff --git a/src/blitter/40bpp_anim.cpp b/src/blitter/40bpp_anim.cpp index 96d8cfa15a..02df4be0a8 100644 --- a/src/blitter/40bpp_anim.cpp +++ b/src/blitter/40bpp_anim.cpp @@ -418,11 +418,11 @@ void Blitter_40bppAnim::CopyFromBuffer(void *video, const void *src, int width, uint8_t *anim_line = ((uint32_t *)video - (uint32_t *)_screen.dst_ptr) + anim_buf; for (; height > 0; height--) { - memcpy(dst, usrc, width * sizeof(uint32_t)); + std::copy_n(usrc, width, dst); usrc += width; dst += _screen.pitch; /* Copy back the anim-buffer */ - memcpy(anim_line, usrc, width * sizeof(uint8_t)); + std::copy_n(reinterpret_cast(usrc), width, anim_line); usrc = (const uint32_t *)((const uint8_t *)usrc + width); anim_line += _screen.pitch; } @@ -440,11 +440,11 @@ void Blitter_40bppAnim::CopyToBuffer(const void *video, void *dst, int width, in const uint8_t *anim_line = ((const uint32_t *)video - (uint32_t *)_screen.dst_ptr) + anim_buf; for (; height > 0; height--) { - memcpy(udst, src, width * sizeof(uint32_t)); + std::copy_n(src, width, udst); src += _screen.pitch; udst += width; /* Copy the anim-buffer */ - memcpy(udst, anim_line, width * sizeof(uint8_t)); + std::copy_n(anim_line, width, reinterpret_cast(udst)); udst = (uint32_t *)((uint8_t *)udst + width); anim_line += _screen.pitch; } @@ -494,7 +494,7 @@ void Blitter_40bppAnim::ScrollBuffer(void *video, int &left, int &top, int &widt uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x); uint th = height - scroll_y; for (; th > 0; th--) { - memcpy(dst, src, tw * sizeof(uint8_t)); + std::copy_n(src, tw, dst); src -= _screen.pitch; dst -= _screen.pitch; } diff --git a/src/blitter/8bpp_base.cpp b/src/blitter/8bpp_base.cpp index fceb5c4adf..131b9519da 100644 --- a/src/blitter/8bpp_base.cpp +++ b/src/blitter/8bpp_base.cpp @@ -56,7 +56,7 @@ void Blitter_8bppBase::CopyFromBuffer(void *video, const void *src, int width, i const uint8_t *usrc = (const uint8_t *)src; for (; height > 0; height--) { - memcpy(dst, usrc, width * sizeof(uint8_t)); + std::copy_n(usrc, width, dst); usrc += width; dst += _screen.pitch; } @@ -68,7 +68,7 @@ void Blitter_8bppBase::CopyToBuffer(const void *video, void *dst, int width, int const uint8_t *src = (const uint8_t *)video; for (; height > 0; height--) { - memcpy(udst, src, width * sizeof(uint8_t)); + std::copy_n(src, width, udst); src += _screen.pitch; udst += width; } @@ -80,7 +80,7 @@ void Blitter_8bppBase::CopyImageToBuffer(const void *video, void *dst, int width const uint8_t *src = (const uint8_t *)video; for (; height > 0; height--) { - memcpy(udst, src, width * sizeof(uint8_t)); + std::copy_n(src, width, udst); src += _screen.pitch; udst += dst_pitch; } @@ -112,7 +112,7 @@ void Blitter_8bppBase::ScrollBuffer(void *video, int &left, int &top, int &width } for (int h = height; h > 0; h--) { - memcpy(dst, src, width * sizeof(uint8_t)); + std::copy_n(src, width, dst); src -= _screen.pitch; dst -= _screen.pitch; } diff --git a/src/blitter/8bpp_optimized.cpp b/src/blitter/8bpp_optimized.cpp index 05169ad9cf..5cc16d975a 100644 --- a/src/blitter/8bpp_optimized.cpp +++ b/src/blitter/8bpp_optimized.cpp @@ -225,7 +225,7 @@ Sprite *Blitter_8bppOptimized::Encode(SpriteType sprite_type, const SpriteLoader dest_sprite->width = root_sprite.width; dest_sprite->x_offs = root_sprite.x_offs; dest_sprite->y_offs = root_sprite.y_offs; - memcpy(dest_sprite->data, temp_dst, size); + std::copy_n(reinterpret_cast(temp_dst), size, dest_sprite->data); return dest_sprite; } diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index 03936edcb2..cb2337d698 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -259,7 +259,7 @@ SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList * if (sockets == nullptr) { this->address_length = (int)runp->ai_addrlen; assert(sizeof(this->address) >= runp->ai_addrlen); - memcpy(&this->address, runp->ai_addr, runp->ai_addrlen); + std::copy_n(reinterpret_cast(runp->ai_addr), runp->ai_addrlen, reinterpret_cast(&this->address)); #ifdef __EMSCRIPTEN__ /* Emscripten doesn't zero sin_zero, but as we compare addresses * to see if they are the same address, we need them to be zero'd. diff --git a/src/network/core/address.h b/src/network/core/address.h index 5263fdacdb..524908610c 100644 --- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -58,11 +58,11 @@ public: * @param address The IP address with port. * @param address_length The length of the address. */ - NetworkAddress(sockaddr *address, int address_length) : + NetworkAddress(const sockaddr *address, int address_length) : address_length(address_length), resolved(address_length != 0) { - memcpy(&this->address, address, address_length); + std::copy_n(reinterpret_cast(address), address_length, reinterpret_cast(&this->address)); } /** diff --git a/src/network/core/host.cpp b/src/network/core/host.cpp index e55a44a024..636fd9dd33 100644 --- a/src/network/core/host.cpp +++ b/src/network/core/host.cpp @@ -48,7 +48,7 @@ static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Wi sockaddr_storage address{}; /* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */ - memcpy(&address, &ifo.iiAddress.Address, sizeof(sockaddr)); + std::copy_n(reinterpret_cast(&ifo.iiAddress.Address), sizeof(sockaddr), reinterpret_cast(&address)); reinterpret_cast(&address)->sin_addr.s_addr = ifo.iiAddress.AddressIn.sin_addr.s_addr | ~ifo.iiNetmask.AddressIn.sin_addr.s_addr; NetworkAddress addr(address, sizeof(sockaddr)); if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const &elem) -> bool { return elem == addr; })) broadcast->push_back(addr); diff --git a/src/network/core/http_curl.cpp b/src/network/core/http_curl.cpp index 920808499e..dcfc7d0a91 100644 --- a/src/network/core/http_curl.cpp +++ b/src/network/core/http_curl.cpp @@ -203,7 +203,7 @@ void HttpThread() /* Copy the buffer out of CURL. OnReceiveData() will free it when done. */ std::unique_ptr buffer = std::make_unique(size * nmemb); - memcpy(buffer.get(), ptr, size * nmemb); + std::copy_n(ptr, size * nmemb, buffer.get()); callback->OnReceiveData(std::move(buffer), size * nmemb); return size * nmemb; diff --git a/src/os/windows/crashlog_win.cpp b/src/os/windows/crashlog_win.cpp index 4ffd2442a9..9adc822c41 100644 --- a/src/os/windows/crashlog_win.cpp +++ b/src/os/windows/crashlog_win.cpp @@ -231,8 +231,7 @@ static const uint MAX_FRAMES = 64; frame.AddrStack.Mode = AddrModeFlat; /* Copy context record as StackWalk64 may modify it. */ - CONTEXT ctx; - memcpy(&ctx, ep->ContextRecord, sizeof(ctx)); + CONTEXT ctx = *ep->ContextRecord; /* Allocate space for symbol info. * The total initialised size must be sufficient for a null-terminating char at sym_info->Name[sym_info->MaxNameLength], diff --git a/src/palette.cpp b/src/palette.cpp index 1665aaf7cf..84fed51898 100644 --- a/src/palette.cpp +++ b/src/palette.cpp @@ -209,7 +209,7 @@ void DoPaletteAnimations(); void GfxInitPalettes() { std::lock_guard lock(_palette_mutex); - memcpy(&_cur_palette, &_palette, sizeof(_cur_palette)); + _cur_palette = _palette; DoPaletteAnimations(); } diff --git a/src/random_access_file.cpp b/src/random_access_file.cpp index f305832053..ba41f8481d 100644 --- a/src/random_access_file.cpp +++ b/src/random_access_file.cpp @@ -146,7 +146,7 @@ void RandomAccessFile::ReadBlock(void *ptr, size_t size) { if (this->buffer != this->buffer_end) { size_t to_copy = std::min(size, this->buffer_end - this->buffer); - memcpy(ptr, this->buffer, to_copy); + std::copy_n(this->buffer, to_copy, static_cast(ptr)); this->buffer += to_copy; size -= to_copy; if (size == 0) return; diff --git a/src/safeguards.h b/src/safeguards.h index f2aa0b37ad..91157590a7 100644 --- a/src/safeguards.h +++ b/src/safeguards.h @@ -47,6 +47,9 @@ #endif #define stricmp SAFEGUARD_DO_NOT_USE_THIS_METHOD +#define memcmp SAFEGUARD_DO_NOT_USE_THIS_METHOD +#define memcpy SAFEGUARD_DO_NOT_USE_THIS_METHOD + /* Use fgets instead. */ #define gets SAFEGUARD_DO_NOT_USE_THIS_METHOD diff --git a/src/screenshot_bmp.cpp b/src/screenshot_bmp.cpp index 4d71d71253..a47058bc15 100644 --- a/src/screenshot_bmp.cpp +++ b/src/screenshot_bmp.cpp @@ -123,7 +123,7 @@ public: while (n-- != 0) { if (pixelformat == 8) { /* Move to 'line', leave last few pixels in line zeroed */ - memcpy(line.data(), buff.data() + n * w, w); + std::copy_n(buff.data() + n * w, w, line.data()); } else { /* Convert from 'native' 32bpp to BMP-like 24bpp. * Works for both big and little endian machines */ diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index 21623ac5fa..9a67cc37b3 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -134,7 +134,7 @@ public: this->CheckAllocationAllowed(size - oldsize); void *new_p = this->DoAlloc(size); - memcpy(new_p, p, std::min(oldsize, size)); + std::copy_n(static_cast(p), std::min(oldsize, size), static_cast(new_p)); this->Free(p, oldsize); return new_p; @@ -261,7 +261,7 @@ void Squirrel::AddMethod(std::string_view method_name, SQFUNCTION proc, std::str if (size != 0) { void *ptr = sq_newuserdata(vm, size); - memcpy(ptr, userdata, size); + std::copy_n(static_cast(userdata), size, static_cast(ptr)); } sq_newclosure(this->vm, proc, size != 0 ? 1 : 0);