From 3afa8937d8bcba6e7be9068753181a9524b3e632 Mon Sep 17 00:00:00 2001 From: Andy Bauer Date: Sat, 19 Nov 2016 08:52:22 -0600 Subject: [PATCH] calculate string lengths and array ndx's properly --- src/zm_monitor.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index da8f86719..ef2e25f9d 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -3989,20 +3989,32 @@ void MonitorStream::runStream() // 15 is the max length for the swap path suffix, /zmswap-whatever, assuming max 6 digits for monitor id const int max_swap_len_suffix = 15; - int swap_path_length = strlen(config.path_swap)+1; // +1 for NULL terminator + int swap_path_length = strlen(config.path_swap) + 1; // +1 for NULL terminator + int subfolder1_length = snprintf(NULL, 0, "/zmswap-m%d", monitor->Id() ) + 1; + int subfolder2_length = snprintf(NULL, 0, "/zmswap-q%06d", connkey ) + 1; + int total_swap_path_length = swap_path_length + subfolder1_length + subfolder2_length; if ( connkey && playback_buffer > 0 ) { - if ( swap_path_length + max_swap_len_suffix > PATH_MAX ) { - Error( "Swap Path is too long. %d > %d ", swap_path_length+max_swap_len_suffix, PATH_MAX ); + if ( total_swap_path_length + max_swap_len_suffix > PATH_MAX ) { + Error( "Swap Path is too long. %d > %d ", total_swap_path_length+max_swap_len_suffix, PATH_MAX ); } else { - swap_path = (char *)malloc( swap_path_length+max_swap_len_suffix ); - Debug( 3, "Checking swap image path %s", config.path_swap ); + swap_path = (char *)malloc( total_swap_path_length+max_swap_len_suffix ); strncpy( swap_path, config.path_swap, swap_path_length ); + + Debug( 3, "Checking swap path folder: %s", swap_path ); if ( checkSwapPath( swap_path, false ) ) { - snprintf( &(swap_path[swap_path_length]), max_swap_len_suffix, "/zmswap-m%d", monitor->Id() ); + // Append the subfolder name /zmswap-m{monitor-id} to the end of swap_path + int ndx = swap_path_length - 1; // Array index of the NULL terminator + snprintf( &(swap_path[ndx]), subfolder1_length, "/zmswap-m%d", monitor->Id() ); + + Debug( 4, "Checking swap path subfolder: %s", swap_path ); if ( checkSwapPath( swap_path, true ) ) { - snprintf( &(swap_path[swap_path_length]), max_swap_len_suffix, "/zmswap-q%06d", connkey ); + // Append the subfolder name /zmswap-q{connection key} to the end of swap_path + ndx = swap_path_length+subfolder1_length - 2; // Array index of the NULL terminator + snprintf( &(swap_path[ndx]), subfolder2_length, "/zmswap-q%06d", connkey ); + + Debug( 4, "Checking swap path subfolder: %s", swap_path ); if ( checkSwapPath( swap_path, true ) ) { buffered_playback = true; }