Partially de-reinterpret_cast XWindowsClipboard

This commit is contained in:
Andrew Nelless 2016-09-09 16:18:05 +01:00
parent 5272c9dde4
commit 5b8fb69124
1 changed files with 39 additions and 36 deletions

View File

@ -31,6 +31,7 @@
#include "common/stdvector.h" #include "common/stdvector.h"
#include <cstdio> #include <cstdio>
#include <cstring>
#include <X11/Xatom.h> #include <X11/Xatom.h>
// //
@ -516,7 +517,7 @@ XWindowsClipboard::icccmFillCache()
} }
XWindowsUtil::convertAtomProperty(data); XWindowsUtil::convertAtomProperty(data);
const Atom* targets = static_cast<const Atom*>(data.data()); const Atom* targets = reinterpret_cast<const Atom*>(data.data()); // TODO: Safe?
const UInt32 numTargets = data.size() / sizeof(Atom); const UInt32 numTargets = data.size() / sizeof(Atom);
LOG((CLOG_DEBUG " available targets: %s", XWindowsUtil::atomsToString(m_display, targets, numTargets).c_str())); LOG((CLOG_DEBUG " available targets: %s", XWindowsUtil::atomsToString(m_display, targets, numTargets).c_str()));
@ -594,7 +595,7 @@ XWindowsClipboard::icccmGetTime() const
String data; String data;
if (icccmGetSelection(m_atomTimestamp, &actualTarget, &data) && if (icccmGetSelection(m_atomTimestamp, &actualTarget, &data) &&
actualTarget == m_atomInteger) { actualTarget == m_atomInteger) {
Time time = *static_cast<const Time*>(data.data()); Time time = *reinterpret_cast<const Time*>(data.data());
LOG((CLOG_DEBUG1 "got ICCCM time %d", time)); LOG((CLOG_DEBUG1 "got ICCCM time %d", time));
return time; return time;
} }
@ -671,11 +672,11 @@ XWindowsClipboard::motifOwnsClipboard() const
} }
// check the owner window against the current clipboard owner // check the owner window against the current clipboard owner
const MotifClipHeader* header = if (data.size() >= sizeof(MotifClipHeader)) {
static_cast<const MotifClipHeader*>(data.data()); MotifClipHeader header;
if (data.size() >= sizeof(MotifClipHeader) && std::memcpy (&header, data.data(), sizeof(header));
header->m_id == kMotifClipHeader) { if ((header.m_id == kMotifClipHeader) &&
if (static_cast<Window>(header->m_selectionOwner) == owner) { (static_cast<Window>(header.m_selectionOwner) == owner)) {
return true; return true;
} }
} }
@ -699,18 +700,18 @@ XWindowsClipboard::motifFillCache()
return; return;
} }
// check that the header is okay MotifClipHeader header;
const MotifClipHeader* header = if (data.size() < sizeof(header)) { // check that the header is okay
static_cast<const MotifClipHeader*>(data.data()); return;
if (data.size() < sizeof(MotifClipHeader) || }
header->m_id != kMotifClipHeader || std::memcpy (&header, data.data(), sizeof(header));
header->m_numItems < 1) { if (header.m_id != kMotifClipHeader || header.m_numItems < 1) {
return; return;
} }
// get the Motif item property from the root window // get the Motif item property from the root window
char name[18 + 20]; char name[18 + 20];
sprintf(name, "_MOTIF_CLIP_ITEM_%d", header->m_item); sprintf(name, "_MOTIF_CLIP_ITEM_%d", header.m_item);
Atom atomItem = XInternAtom(m_display, name, False); Atom atomItem = XInternAtom(m_display, name, False);
data = ""; data = "";
if (!XWindowsUtil::getWindowProperty(m_display, root, if (!XWindowsUtil::getWindowProperty(m_display, root,
@ -719,18 +720,19 @@ XWindowsClipboard::motifFillCache()
return; return;
} }
// check that the item is okay MotifClipItem item;
const MotifClipItem* item = if (data.size() < sizeof(item)) { // check that the item is okay
static_cast<const MotifClipItem*>(data.data()); return;
if (data.size() < sizeof(MotifClipItem) || }
item->m_id != kMotifClipItem || std::memcpy (&item, data.data(), sizeof(item));
item->m_numFormats - item->m_numDeletedFormats < 1) { if (item.m_id != kMotifClipItem ||
item.m_numFormats - item.m_numDeletedFormats < 1) {
return; return;
} }
// format list is after static item structure elements // format list is after static item structure elements
const SInt32 numFormats = item->m_numFormats - item->m_numDeletedFormats; const SInt32 numFormats = item.m_numFormats - item.m_numDeletedFormats;
const SInt32* formats = static_cast<const SInt32*>(item->m_size + const SInt32* formats = reinterpret_cast<const SInt32*>(item.m_size +
static_cast<const char*>(data.data())); static_cast<const char*>(data.data()));
// get the available formats // get the available formats
@ -748,18 +750,20 @@ XWindowsClipboard::motifFillCache()
} }
// check that the format is okay // check that the format is okay
const MotifClipFormat* motifFormat = MotifClipFormat motifFormat;
static_cast<const MotifClipFormat*>(data.data()); if (data.size() < sizeof(motifFormat)) {
if (data.size() < sizeof(MotifClipFormat) || continue;
motifFormat->m_id != kMotifClipFormat || }
motifFormat->m_length < 0 || std::memcpy (&motifFormat, data.data(), sizeof(motifFormat));
motifFormat->m_type == None || if (motifFormat.m_id != kMotifClipFormat ||
motifFormat->m_deleted != 0) { motifFormat.m_length < 0 ||
motifFormat.m_type == None ||
motifFormat.m_deleted != 0) {
continue; continue;
} }
// save it // save it
motifFormats.insert(std::make_pair(motifFormat->m_type, data)); motifFormats.insert(std::make_pair(motifFormat.m_type, data));
} }
//const UInt32 numMotifFormats = motifFormats.size(); //const UInt32 numMotifFormats = motifFormats.size();
@ -782,15 +786,14 @@ XWindowsClipboard::motifFillCache()
} }
// get format // get format
const MotifClipFormat* motifFormat = MotifClipFormat motifFormat;
static_cast<const MotifClipFormat*>( std::memcpy (&motifFormat, index2->second.data(), sizeof(motifFormat));
index2->second.data()); const Atom target = motifFormat.m_type;
const Atom target = motifFormat->m_type;
// get the data (finally) // get the data (finally)
Atom actualTarget; Atom actualTarget;
String targetData; String targetData;
if (!motifGetSelection(motifFormat, &actualTarget, &targetData)) { if (!motifGetSelection(&motifFormat, &actualTarget, &targetData)) {
LOG((CLOG_DEBUG1 " no data for target %s", XWindowsUtil::atomToString(m_display, target).c_str())); LOG((CLOG_DEBUG1 " no data for target %s", XWindowsUtil::atomToString(m_display, target).c_str()));
continue; continue;
} }
@ -855,7 +858,7 @@ XWindowsClipboard::insertMultipleReply(Window requestor,
// data is a list of atom pairs: target, property // data is a list of atom pairs: target, property
XWindowsUtil::convertAtomProperty(data); XWindowsUtil::convertAtomProperty(data);
const Atom* targets = static_cast<const Atom*>(data.data()); const Atom* targets = reinterpret_cast<const Atom*>(data.data());
const UInt32 numTargets = data.size() / sizeof(Atom); const UInt32 numTargets = data.size() / sizeof(Atom);
// add replies for each target // add replies for each target