From c15cb0ba2467e6f20353f136026553db2e5d5b0d Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Sat, 24 Nov 2018 23:43:09 +0100 Subject: [PATCH] memory management + handling environment variables with numbers --- ChangeLog | 3 ++- build.sh | 2 +- examples/transparent.cpp | 37 +++++++++++++++++++++--------------- src/foptimove.cpp | 6 ++++-- src/fterm.cpp | 31 ++++++++++++++++++++++++++---- test/ftermdetection-test.cpp | 2 +- 6 files changed, 57 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index e6b04a2d..f755c339 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,7 +7,8 @@ 2018-11-18 Markus Gans * The FListViewItem class now provides checkable list view items - * Adding the checklist example to demonstrate the checkable FListViewItems + * Adding the checklist example to demonstrate the checkable + FListViewItems * A checkable FListViewItem now shows the input cursor 2018-11-12 Markus Gans diff --git a/build.sh b/build.sh index ae0c9e42..f711d2b3 100755 --- a/build.sh +++ b/build.sh @@ -43,7 +43,7 @@ fi # Build commands case "$1" in "--release"|"release") - if ! ./configure --prefix="$PREFIX" CXXFLAGS="-O3 -fno-rtti" + if ! ./configure --prefix="$PREFIX" CXXFLAGS="-O2" # "-O3 -fno-rtti" then echo "${RED}Configure failed!${NORMAL}" 1>&2 exit -1 diff --git a/examples/transparent.cpp b/examples/transparent.cpp index 3672c440..ddc04a55 100644 --- a/examples/transparent.cpp +++ b/examples/transparent.cpp @@ -184,9 +184,9 @@ class MainWindow : public finalcut::FDialog // Data Members finalcut::FString line1; finalcut::FString line2; - Transparent transpwin; - Transparent shadowwin; - Transparent ibg; + Transparent* transpwin; + Transparent* shadowwin; + Transparent* ibg; finalcut::FStatusBar status_bar; }; #pragma pack(pop) @@ -196,25 +196,32 @@ MainWindow::MainWindow (finalcut::FWidget* parent) : FDialog(parent) , line1() , line2() - , transpwin(this) - , shadowwin(this, Transparent::shadow) - , ibg(this, Transparent::inherit_background) + , transpwin(0) + , shadowwin(0) + , ibg(0) , status_bar(this) { line1 = " .-. .-. .-."; line2 = "`._.' `._.' `._.' "; - transpwin.setText("transparent"); - transpwin.setGeometry (6, 3, 29, 12); - transpwin.unsetTransparentShadow(); + // The memory allocation for the following three sub windows occurs + // with the operator new. The lifetime of the generated widget + // is managed by the parent object (this). The operator delete + // is not required in this scope and would result in a double free. + transpwin = new Transparent(this); + transpwin->setText("transparent"); + transpwin->setGeometry (6, 3, 29, 12); + transpwin->unsetTransparentShadow(); - shadowwin.setText("shadow"); - shadowwin.setGeometry (46, 11, 29, 12); - shadowwin.unsetTransparentShadow(); + shadowwin = new Transparent(this, Transparent::shadow); + shadowwin->setText("shadow"); + shadowwin->setGeometry (46, 11, 29, 12); + shadowwin->unsetTransparentShadow(); - ibg.setText("inherit background"); - ibg.setGeometry (42, 3, 29, 7); - ibg.unsetTransparentShadow(); + ibg = new Transparent(this, Transparent::inherit_background); + ibg->setText("inherit background"); + ibg->setGeometry (42, 3, 29, 7); + ibg->unsetTransparentShadow(); // Statusbar at the bottom status_bar.setMessage("Press Q to quit"); diff --git a/src/foptimove.cpp b/src/foptimove.cpp index 41c236fc..e5928d1a 100644 --- a/src/foptimove.cpp +++ b/src/foptimove.cpp @@ -646,11 +646,13 @@ int FOptiMove::repeatedAppend ( const capability& o if ( dst ) { dst += dst_len; + std::size_t free = BUF_SIZE - dst_len; while ( count-- > 0 ) { - std::strncpy (dst, o.cap, src_len + 1); + std::strncpy (dst, o.cap, free); dst += src_len; + free -= src_len; } } } @@ -793,7 +795,7 @@ inline int FOptiMove::horizontalMove (char hmove[], int from_x, int to_x) // Move to fixed column position1 std::strncat ( hmove , tparm(F_column_address.cap, to_x, 0, 0, 0, 0, 0, 0, 0, 0) - , BUF_SIZE - std::strlen(hmove) - 1); + , BUF_SIZE - std::strlen(hmove) - 1 ); hmove[BUF_SIZE - 1] = '\0'; htime = F_column_address.duration; } diff --git a/src/fterm.cpp b/src/fterm.cpp index af1f6205..4b40ce58 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -63,6 +63,9 @@ FMouseControl* FTerm::mouse = 0; FTermDebugData* FTerm::debug_data = 0; #endif +// function prototypes +uInt env2uint (const char*); + //---------------------------------------------------------------------- // class FTerm @@ -487,11 +490,11 @@ void FTerm::detectTermSize() { term_geometry.setPos (1, 1); // Use COLUMNS or fallback to the xterm default width of 80 characters - char* Columns = std::getenv("COLUMNS"); - term_geometry.setWidth(Columns ? std::size_t(std::atoi(Columns)) : 80); + uInt Columns = env2uint ("COLUMNS"); + term_geometry.setWidth( ( Columns == 0) ? 80 : Columns); // Use LINES or fallback to the xterm default height of 24 characters - char* Lines = std::getenv("LINES"); - term_geometry.setHeight(Lines ? std::size_t(std::atoi(Lines)) : 24); + uInt Lines = env2uint ("LINES"); + term_geometry.setHeight( ( Lines == 0 ) ? 24 : Lines); } else { @@ -2072,6 +2075,26 @@ void FTerm::signal_handler (int signum) , strsignal(signum) ); std::terminate(); } + +} + +// FTerm non-member functions +//---------------------------------------------------------------------- +uInt env2uint (const char* env) +{ + FString str(env); + + if ( str.isEmpty() ) + return 0; + + try + { + return str.toUInt(); + } + catch (const std::exception& ex) + { + return 0; + } } } // namespace finalcut diff --git a/test/ftermdetection-test.cpp b/test/ftermdetection-test.cpp index 027b586e..2fff2390 100644 --- a/test/ftermdetection-test.cpp +++ b/test/ftermdetection-test.cpp @@ -2314,7 +2314,7 @@ void FTermDetectionTest::terminalSimulation (console con) { len = read (fd_stdin, buffer, sizeof(buffer)); - if ( len != -1 && std::size_t(len) < sizeof(buffer) ) + if ( len > 0 && std::size_t(len) < sizeof(buffer) ) { buffer[len] = '\0'; write (fd_master, buffer, len); // Send data to the master side