Added parameter checks to strn0cpy() and Utf8Strn0Cpy()

This commit is contained in:
Klaus Schmidinger 2025-01-15 08:57:45 +01:00
parent 657e5dda5d
commit a7576f0b6c
3 changed files with 22 additions and 13 deletions

View File

@ -3374,6 +3374,7 @@ Stefan Hofmann <stefan.hofmann@t-online.de>
for fixing some typos in the translation files for fixing some typos in the translation files
for adding 1 to Utf8BufSize() for worst case for adding 1 to Utf8BufSize() for worst case
for adding a header to the backtrace for adding a header to the backtrace
for adding parameter checks to strn0cpy()
Stefan Blochberger <Stefan.Blochberger@gmx.de> Stefan Blochberger <Stefan.Blochberger@gmx.de>
for suggesting to automatically display the progress display whenever replay of a for suggesting to automatically display the progress display whenever replay of a

View File

@ -10053,3 +10053,4 @@ Video Disk Recorder Revision History
- Fixed handling margins for timers that are not VPS controlled and not spawned (thanks - Fixed handling margins for timers that are not VPS controlled and not spawned (thanks
to Markus Ehrnsperger). to Markus Ehrnsperger).
- Added a header to the backtrace (thanks to Stefan Hofmann). - Added a header to the backtrace (thanks to Stefan Hofmann).
- Added parameter checks to strn0cpy() (thanks to Stefan Hofmann). Same for Utf8Strn0Cpy().

33
tools.c
View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: tools.c 5.14 2024/09/01 20:43:40 kls Exp $ * $Id: tools.c 5.15 2025/01/15 08:57:45 kls Exp $
*/ */
#include "tools.h" #include "tools.h"
@ -131,8 +131,11 @@ char *strcpyrealloc(char *dest, const char *src)
char *strn0cpy(char *dest, const char *src, size_t n) char *strn0cpy(char *dest, const char *src, size_t n)
{ {
char *s = dest; char *s = dest;
for ( ; --n && (*dest = *src) != 0; dest++, src++) ; if (dest && n) {
*dest = 0; if (src)
for ( ; --n && (*dest = *src) != 0; dest++, src++) ;
*dest = 0;
}
return s; return s;
} }
@ -914,17 +917,21 @@ char *Utf8Strn0Cpy(char *Dest, const char *Src, int n)
if (cCharSetConv::SystemCharacterTable()) if (cCharSetConv::SystemCharacterTable())
return strn0cpy(Dest, Src, n); return strn0cpy(Dest, Src, n);
char *d = Dest; char *d = Dest;
while (*Src) { if (Dest && n > 0) {
int sl = Utf8CharLen(Src); if (Src) {
n -= sl; while (*Src) {
if (n > 0) { int sl = Utf8CharLen(Src);
while (sl--) n -= sl;
*d++ = *Src++; if (n > 0) {
} while (sl--)
else *d++ = *Src++;
break; }
else
break;
}
} }
*d = 0; *d = 0;
}
return Dest; return Dest;
} }