From a7576f0b6c71880bd410475682f52db2a85f538d Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Wed, 15 Jan 2025 08:57:45 +0100 Subject: [PATCH] Added parameter checks to strn0cpy() and Utf8Strn0Cpy() --- CONTRIBUTORS | 1 + HISTORY | 1 + tools.c | 33 ++++++++++++++++++++------------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index ffad2c81..24f2cf90 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -3374,6 +3374,7 @@ Stefan Hofmann for fixing some typos in the translation files for adding 1 to Utf8BufSize() for worst case for adding a header to the backtrace + for adding parameter checks to strn0cpy() Stefan Blochberger for suggesting to automatically display the progress display whenever replay of a diff --git a/HISTORY b/HISTORY index db6b548b..f6430326 100644 --- a/HISTORY +++ b/HISTORY @@ -10053,3 +10053,4 @@ Video Disk Recorder Revision History - Fixed handling margins for timers that are not VPS controlled and not spawned (thanks to Markus Ehrnsperger). - Added a header to the backtrace (thanks to Stefan Hofmann). +- Added parameter checks to strn0cpy() (thanks to Stefan Hofmann). Same for Utf8Strn0Cpy(). diff --git a/tools.c b/tools.c index bb9433c1..f189bb0f 100644 --- a/tools.c +++ b/tools.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * 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" @@ -131,8 +131,11 @@ char *strcpyrealloc(char *dest, const char *src) char *strn0cpy(char *dest, const char *src, size_t n) { char *s = dest; - for ( ; --n && (*dest = *src) != 0; dest++, src++) ; - *dest = 0; + if (dest && n) { + if (src) + for ( ; --n && (*dest = *src) != 0; dest++, src++) ; + *dest = 0; + } return s; } @@ -914,17 +917,21 @@ char *Utf8Strn0Cpy(char *Dest, const char *Src, int n) if (cCharSetConv::SystemCharacterTable()) return strn0cpy(Dest, Src, n); char *d = Dest; - while (*Src) { - int sl = Utf8CharLen(Src); - n -= sl; - if (n > 0) { - while (sl--) - *d++ = *Src++; - } - else - break; + if (Dest && n > 0) { + if (Src) { + while (*Src) { + int sl = Utf8CharLen(Src); + n -= sl; + if (n > 0) { + while (sl--) + *d++ = *Src++; + } + else + break; + } } - *d = 0; + *d = 0; + } return Dest; }