Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation fixes and memory corruption prevention #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ajadriver/linux/ntv2devicefeatures.c
1 change: 1 addition & 0 deletions ajadriver/linux/ntv2driverprocamp.c
1 change: 1 addition & 0 deletions ajadriver/linux/ntv2vpidfromspec.c
7 changes: 5 additions & 2 deletions ajalibraries/ajabase/common/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ AJABuffer::AllocateBuffer(size_t size, size_t alignment, char* pName)

ComputeAlignment();

size_t nameLen = strlen(pName);
const size_t nameLen = strlen(pName);
mpAllocateName = new char[nameLen + 1];
AJA_ASSERT(mpAllocateName != NULL);
strncpy(mpAllocateName, pName, nameLen);

memcpy(mpAllocateName, pName, nameLen);
mpAllocateName[nameLen] = '\0';

}
// allocate a non shared buffer
else
Expand Down
4 changes: 0 additions & 4 deletions ajalibraries/ajantv2/includes/ntv2devicefeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#ifndef NTV2DEVICEFEATURES_H
#define NTV2DEVICEFEATURES_H

#if defined(AJALinux) || defined(AJA_LINUX)
#include <stddef.h> // For size_t
#endif

#include "ajaexport.h"
#include "ajatypes.h"
#include "ntv2enums.h"
Expand Down
65 changes: 65 additions & 0 deletions ajalibraries/ajantv2/includes/ntv2driverprocamp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* SPDX-License-Identifier: MIT */
/**
@file ntv2driverprocamp.h
@brief Declares functions used in the NTV2 device driver.
@copyright (C) 2004-2021 AJA Video Systems, Inc. All rights reserved.
**/
// ntv2driverprocamp.h
#ifndef NTV2DRIVERPROCAMP_H
#define NTV2DRIVERPROCAMP_H

#include "ajatypes.h"
#include "ntv2enums.h"
#include "ntv2publicinterface.h"

// This file is used by the Linux driver which is C, not C++.
#if defined(__CPLUSPLUS__) || defined(__cplusplus)
#else
#define false (0)
#define true (!false)
#endif

#ifdef AJALinux
#include "ntv2linuxpublicinterface.h"
#endif

#ifdef AJAMac
#include <IOKit/IOTypes.h>
#include "ntv2macpublicinterface.h"
#endif

#ifdef MSWindows
#include "ntv2winpublicinterface.h"
#endif


bool SetVirtualProcampRegister( VirtualRegisterNum virtualRegisterNum,
ULWord value,
VirtualProcAmpRegisters *regs);

bool GetVirtualProcampRegister( VirtualRegisterNum virtualRegisterNum,
ULWord *value,
VirtualProcAmpRegisters *regs);

#ifndef AJAMac // note: in Mac-land these are methods in MacDriver.h

bool WriteHardwareProcampRegister( ULWord inDeviceIndex,
NTV2DeviceID deviceID,
VirtualRegisterNum virtualRegisterNum,
ULWord value,
HardwareProcAmpRegisterImage *hwRegImage);

bool RestoreHardwareProcampRegisters( ULWord inDeviceIndex,
NTV2DeviceID deviceID,
VirtualProcAmpRegisters *regs,
HardwareProcAmpRegisterImage *hwRegImage);

bool I2CWriteDataSingle(ULWord inDeviceIndex, UByte I2CAddress, UByte I2CData);

bool I2CWriteDataDoublet( ULWord inDeviceIndex,
UByte I2CAddress1, UByte I2CData1,
UByte I2CAddress2, UByte I2CData2);

#endif // !AJAMac

#endif // NTV2DRIVERPROCAMP_H
2 changes: 1 addition & 1 deletion ajalibraries/ajantv2/src/ntv2config2022.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ bool CNTV2Config2022::GetLinkStatus(eSFP sfp, SFPStatus & sfpStatus)

bool CNTV2Config2022::Get2022ChannelRxStatus(NTV2Channel channel, s2022RxChannelStatus & chanStatus)
{
uint32_t addr;
uint32_t addr = 0;

SelectRxChannel(channel, SFP_2, addr);
ReadChannelRegister(addr + kReg2022_6_rx_sec_recv_pkt_cnt, &chanStatus.sfp2RxPackets);
Expand Down
6 changes: 4 additions & 2 deletions ajalibraries/ajantv2/src/ntv2discover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ extractBoardInventory(NTV2NubPkt *pPkt, NTV2DiscoverRespPayload *boardInventory,
dbi->boardID = ntohl(dbi->boardID);
char tmp[NTV2_DISCOVER_BOARDINFO_DESC_STRMAX];
// Prepend peername to description.
strncpy(tmp, dbi->description, NTV2_DISCOVER_BOARDINFO_DESC_STRMAX);

int peernameLen = snprintf( dbi->description,
strncpy(tmp, dbi->description, NTV2_DISCOVER_BOARDINFO_DESC_STRMAX - 1);
tmp[sizeof tmp - 1] = '\0';

int peernameLen = snprintf( dbi->description,
NTV2_DISCOVER_BOARDINFO_DESC_STRMAX,
"%s: ", peername);

Expand Down
18 changes: 13 additions & 5 deletions ajalibraries/ajantv2/src/ntv2driverinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ bool CNTV2DriverInterface::BitstreamStatus (NTV2ULWordVector & outRegValues)
void CNTV2DriverInterface::FinishOpen (void)
{
// HACK! FinishOpen needs frame geometry to determine frame buffer size and number.
NTV2FrameGeometry fg;
NTV2FrameGeometry fg{};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This initializer won't work if the library is built without C++11 compiler support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, but I thought new versions of NTV2 were C++11+ only?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Believe me, we'd love to pull that trigger, but we have a surprising number of customers that are stuck with older hardware and operating systems yet still need to use our newer hardware. In SDK 16.0, we changed to assume (by default) C++11 support, but the library can be built without it (by commenting out the lines defining the NTV2_USE_CPLUSPLUS11 and AJA_USE_CPLUSPLUS11 macros).

Copy link
Contributor

@paulh-aja paulh-aja Apr 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the reason Mr. Bill mentioned, in my own code I have preferred initializing NTV2 enum types to either their equivalent zero-value, or their UNKNOWN or INVALID value, ex:

NTV2FrameGeometry fg = NTV2_FG_1920x1080; // default 0-value for NTV2FrameGeometry
NTV2VideoFormat vf = NTV2_FORMAT_UNKNOWN; // default 0-value for NTV2VideoFormat
NTV2PixelFormat pf = NTV2_FBF_FIRST; // default 0-value for NTV2FrameBufferFormat/NTV2PixelFormat

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrbillaja Ahh, okay, I understand. That is unfortunate but understandable. In that case you can either do that or for other structs with less well-defined default values, it's often a safe bet to just do struct MyStruct Name = { 0 }; since 0 is also acceptable as an initializer for pointers and floats (though implicitly converted), and the compiler will zero out the rest as long as something is initialized explicitly.

ULWord val1(0), val2(0);
ReadRegister (kRegGlobalControl, fg, kRegMaskGeometry, kRegShiftGeometry); // Read FrameGeometry
ReadRegister (kRegCh1Control, val1, kRegMaskFrameFormat, kRegShiftFrameFormat); // Read PixelFormat
Expand Down Expand Up @@ -1075,10 +1075,18 @@ bool CNTV2DriverInterface::ParseFlashHeader (BITFILE_INFO_STRUCT & bitFileInfo)
headerError = fileInfo.ParseHeaderFromBuffer(bitFileHdrBuffer);
if (headerError.empty())
{
::strncpy(bitFileInfo.dateStr, fileInfo.GetDate().c_str(), NTV2_BITFILE_DATETIME_STRINGLENGTH);
::strncpy(bitFileInfo.timeStr, fileInfo.GetTime().c_str(), NTV2_BITFILE_DATETIME_STRINGLENGTH);
::strncpy(bitFileInfo.designNameStr, fileInfo.GetDesignName().c_str(), NTV2_BITFILE_DESIGNNAME_STRINGLENGTH);
::strncpy(bitFileInfo.partNameStr, fileInfo.GetPartName().c_str(), NTV2_BITFILE_PARTNAME_STRINGLENGTH);
::strncpy(bitFileInfo.dateStr, fileInfo.GetDate().c_str(), NTV2_BITFILE_DATETIME_STRINGLENGTH - 1);
bitFileInfo.dateStr[NTV2_BITFILE_DATETIME_STRINGLENGTH - 1] = '\0';

::strncpy(bitFileInfo.timeStr, fileInfo.GetTime().c_str(), NTV2_BITFILE_DATETIME_STRINGLENGTH - 1);
bitFileInfo.timeStr[NTV2_BITFILE_DATETIME_STRINGLENGTH - 1] = '\0';

::strncpy(bitFileInfo.designNameStr, fileInfo.GetDesignName().c_str(), NTV2_BITFILE_DESIGNNAME_STRINGLENGTH - 1);
bitFileInfo.designNameStr[NTV2_BITFILE_DESIGNNAME_STRINGLENGTH - 1] = '\0';

::strncpy(bitFileInfo.partNameStr, fileInfo.GetPartName().c_str(), NTV2_BITFILE_PARTNAME_STRINGLENGTH - 1);
bitFileInfo.partNameStr[NTV2_BITFILE_PARTNAME_STRINGLENGTH - 1] = '\0';

bitFileInfo.numBytes = ULWord(fileInfo.GetProgramStreamLength());
}
return headerError.empty();
Expand Down
Loading