diff --git a/Framework/Crystal/src/IntegratePeakTimeSlices.cpp b/Framework/Crystal/src/IntegratePeakTimeSlices.cpp index 70e398ef3d65ea8c30d3b112373b7e929153f56e..9828bea2f54bfa54db588459eca4c19b2d772842 100644 --- a/Framework/Crystal/src/IntegratePeakTimeSlices.cpp +++ b/Framework/Crystal/src/IntegratePeakTimeSlices.cpp @@ -587,6 +587,12 @@ void IntegratePeakTimeSlices::exec() { // Now set up the center for this peak int i = find("Mrow", names); + if (i < 0) { + throw std::runtime_error("Inconsistency found in algorithm " + "execution. The index for the parameter " + "Mrow is negative."); + } + lastRow = (int)(params[i] + .5); i = find("Mcol", names); lastCol = (int)(params[i] + .5); @@ -2220,7 +2226,16 @@ bool IntegratePeakTimeSlices::isGoodFit(std::vector<double> const ¶ms, std::vector<std::string> const &names, double chisqOverDOF) { int Ibk = find("Background", names); + if (Ibk < 0) + throw std::runtime_error( + "Irrecoverable inconsistency found. The index for the " + "parameter 'Background' is lower than zero."); + int IIntensity = find("Intensity", names); + if (IIntensity < 0) + throw std::runtime_error( + "Irrecoverable inconsistency found. The index for the " + "parameter 'Intensity' is lower than zero."); if (chisqOverDOF < 0) { @@ -2498,6 +2513,15 @@ int IntegratePeakTimeSlices::UpdateOutputWS( int Irow = find("Mrow", names); int Icol = find("Mcol", names); + if (Ibk < 0 || IIntensity < 0 || IVx < 0 || IVy < 0 || IVxy < 0 || Irow < 0 || + Icol < 0) { + throw std::runtime_error("Inconsistency found when updating output " + "workspace. None of the indices for the " + "parameters 'Background', 'Intensity', 'SScol', " + "'SSrow', 'SSrc', 'Mrow', 'Mcol' can be " + "negative."); + } + int newRowIndex = 0; if (dir > 0) diff --git a/Framework/CurveFitting/src/Algorithms/RefinePowderInstrumentParameters3.cpp b/Framework/CurveFitting/src/Algorithms/RefinePowderInstrumentParameters3.cpp index a5c35c1f331d71bca31643d8fc85b77d7bf7b2fd..f87a83a0013f454452b40c588a6b9787401d477a 100644 --- a/Framework/CurveFitting/src/Algorithms/RefinePowderInstrumentParameters3.cpp +++ b/Framework/CurveFitting/src/Algorithms/RefinePowderInstrumentParameters3.cpp @@ -870,8 +870,15 @@ double RefinePowderInstrumentParameters3::calculateFunctionError( // 2. Fit with zero iteration double chi2; string fitstatus; - doFitFunction(function, dataws, wsindex, "Levenberg-MarquardtMD", 0, chi2, - fitstatus); + const std::string minimizer = "Levenberg-MarquardtMD"; + bool fitOK = + doFitFunction(function, dataws, wsindex, minimizer, 0, chi2, fitstatus); + + if (!fitOK) { + g_log.warning() << "Fit by " << minimizer + << " with 0 iterations failed, with reason: " << fitstatus + << "\n"; + } // 3. Restore the fit/fix setup for (size_t i = 0; i < parnames.size(); ++i) { diff --git a/Framework/DataHandling/src/CheckMantidVersion.cpp b/Framework/DataHandling/src/CheckMantidVersion.cpp index 30a620c6079eaca94651b3e22d92d059b72a93cf..01776379510adfeda238fd768ab2a72cf9949863 100644 --- a/Framework/DataHandling/src/CheckMantidVersion.cpp +++ b/Framework/DataHandling/src/CheckMantidVersion.cpp @@ -107,11 +107,35 @@ void CheckMantidVersion::exec() { if (!json.empty()) { Json::Reader r; Json::Value root; - r.parse(json, root); + bool parseOK = r.parse(json, root); + if (!parseOK) { + // just warning. The parser is able to get relevant info even if there are + // formatting issues like missing quotes or brackets. + g_log.warning() << "Error found when parsing version information " + "retrieved from GitHub as a JSON string. " + "Error trying to parse this JSON string: " << json + << std::endl + << ". Parsing error details: " + << r.getFormattedErrorMessages() << std::endl; + } - std::string gitHubVersionTag = root["tag_name"].asString(); - mostRecentVersion = cleanVersionTag(gitHubVersionTag); + std::string gitHubVersionTag; + try { + gitHubVersionTag = root["tag_name"].asString(); + } catch (std::runtime_error &re) { + g_log.error() + << "Error while trying to get the field 'tag_name' from " + "the version information retrieved from GitHub. This " + "algorithm cannot continue and will stop now. Error details: " + << re.what() << std::endl; + + mostRecentVersion = "Could not get information from GitHub"; + setProperty("MostRecentVersion", mostRecentVersion); + setProperty("IsNewVersionAvailable", isNewVersionAvailable); + return; + } + mostRecentVersion = cleanVersionTag(gitHubVersionTag); isNewVersionAvailable = isVersionMoreRecent(currentVersion, mostRecentVersion); if (isNewVersionAvailable) { diff --git a/Framework/DataHandling/src/GroupDetectors2.cpp b/Framework/DataHandling/src/GroupDetectors2.cpp index 4bfba12fc4a42003b33a2c65d620b6507a154fe0..ff3e821142095f905fda8b80a02507ba33457a8b 100644 --- a/Framework/DataHandling/src/GroupDetectors2.cpp +++ b/Framework/DataHandling/src/GroupDetectors2.cpp @@ -804,6 +804,10 @@ void GroupDetectors2::readFile(spec2index_map &specs2index, std::istream &File, numberOfSpectra = readInt(thisLine); } while (numberOfSpectra == EMPTY_LINE); + if (numberOfSpectra <= 0) { + throw std::invalid_argument("The number of spectra is zero or negative"); + } + // the value of this map is the list of spectra numbers that will be // combined into a group m_GroupSpecInds[spectrumNo].reserve(numberOfSpectra); diff --git a/Framework/DataHandling/src/LoadRaw/isisraw.cpp b/Framework/DataHandling/src/LoadRaw/isisraw.cpp index 6037ec23a60ae12f7ffbcacb65f8b3695f64bbac..475c96358d3637d62e0030ac41939bca61816f33 100644 --- a/Framework/DataHandling/src/LoadRaw/isisraw.cpp +++ b/Framework/DataHandling/src/LoadRaw/isisraw.cpp @@ -460,7 +460,17 @@ int ISISRAW::ioRAW(FILE *file, bool from_file, bool read_data) { // ioRAW(file, &u_len, 1, from_file); if (from_file) { u_len = add.ad_data - add.ad_user - 2; + + if (u_len < 0 || (add.ad_data < add.ad_user + 2)) { + // this will/would be used for memory allocation + std::cerr << "Error in u_len value read from file, it would be " << u_len + << "; where it is calculated as " + "u_len = ad_data - ad_user - 2, where ad_data: " + << add.ad_data << ", ad_user: " << add.ad_user << std::endl; + return 0; + } } + ioRAW(file, &u_dat, u_len, from_file); ioRAW(file, &ver8, 1, from_file); fgetpos(file, &dhdr_pos); @@ -474,8 +484,12 @@ int ISISRAW::ioRAW(FILE *file, bool from_file, bool read_data) { if (from_file) { ndes = t_nper * (t_nsp1 + 1); ioRAW(file, &ddes, ndes, from_file); - for (i = 0; i < ndes; i++) - fseek(file, 4 * ddes[i].nwords, SEEK_CUR); + for (i = 0; i < ndes; i++) { + int zero = fseek(file, 4 * ddes[i].nwords, SEEK_CUR); + if (0 != zero) + std::cerr << "Failed to seek position in file for index: " << i + << "\n"; + } } } else if (dhdr.d_comp == 0) { ndata = t_nper * (t_nsp1 + 1) * (t_ntc1 + 1); @@ -527,15 +541,38 @@ int ISISRAW::ioRAW(FILE *file, bool from_file, bool read_data) { dhdr.d_exp_filesize = uncomp_filesize / 128; // in 512 byte blocks (vms default allocation unit) - fgetpos(file, &keep_pos); + int zero = fgetpos(file, &keep_pos); + if (!zero) { + std::cerr << "Error when getting file position: " << strerror(errno) + << std::endl; + return -1; + } + // update section addresses - fsetpos(file, &add_pos); + zero = fsetpos(file, &add_pos); + if (!zero) { + std::cerr << "Error when setting file position: " << strerror(errno) + << std::endl; + return -1; + } + ioRAW(file, &add, 1, from_file); // update data header and descriptors etc. - fsetpos(file, &dhdr_pos); + zero = fsetpos(file, &dhdr_pos); + if (!zero) { + std::cerr << "Error when setting file position to header: " + << strerror(errno) << std::endl; + return -1; + } + ioRAW(file, &dhdr, 1, from_file); ioRAW(file, &ddes, ndes, from_file); - fsetpos(file, &keep_pos); + zero = fsetpos(file, &keep_pos); + if (!zero) { + std::cerr << "Error when restoring file position: " << strerror(errno) + << std::endl; + return -1; + } } return 0; } @@ -669,16 +706,18 @@ int ISISRAW::ioRAW(FILE *file, LOG_LINE *s, int len, bool from_file) { /// stuff int ISISRAW::ioRAW(FILE *file, char *s, int len, bool from_file) { - size_t n; if ((len <= 0) || (s == 0)) { return 0; } + + size_t n; if (from_file) { n = fread(s, sizeof(char), len, file); return static_cast<int>(n - len); } else { n = fwrite(s, sizeof(char), len, file); } + return 0; } @@ -687,11 +726,15 @@ int ISISRAW::ioRAW(FILE *file, int *s, int len, bool from_file) { if ((len <= 0) || (s == 0)) { return 0; } + + size_t n; if (from_file) { - fread(s, sizeof(int), len, file); + n = fread(s, sizeof(int), len, file); + return static_cast<int>(n - len); } else { - fwrite(s, sizeof(int), len, file); + n = fwrite(s, sizeof(int), len, file); } + return 0; } @@ -700,10 +743,13 @@ int ISISRAW::ioRAW(FILE *file, uint32_t *s, int len, bool from_file) { if ((len <= 0) || (s == 0)) { return 0; } + + size_t n; if (from_file) { - fread(s, sizeof(uint32_t), len, file); + n = fread(s, sizeof(uint32_t), len, file); + return static_cast<int>(n - len); } else { - fwrite(s, sizeof(uint32_t), len, file); + n = fwrite(s, sizeof(uint32_t), len, file); } return 0; } @@ -714,12 +760,15 @@ int ISISRAW::ioRAW(FILE *file, float *s, int len, bool from_file) { if ((len <= 0) || (s == 0)) { return 0; } + + size_t n; if (from_file) { - fread(s, sizeof(float), len, file); + n = fread(s, sizeof(float), len, file); vaxf_to_local(s, &len, &errcode); + return static_cast<int>(n - len); } else { local_to_vaxf(s, &len, &errcode); - fwrite(s, sizeof(float), len, file); + n = fwrite(s, sizeof(float), len, file); vaxf_to_local(s, &len, &errcode); } return 0; @@ -900,36 +949,6 @@ int ISISRAW::readFromFile(const char *filename, bool read_data) { } } -/// stuff -int ISISRAW::writeToFile(const char *filename) { - unsigned char zero_pad[512]; - memset(zero_pad, 0, sizeof(zero_pad)); - remove(filename); -#ifdef MS_VISUAL_STUDIO - FILE *output_file = NULL; - if (fopen_s(&output_file, filename, "w+bc") != 0) { - return -1; - } -#else //_WIN32 - FILE *output_file = fopen(filename, "w+bc"); -#endif //_WIN32 - if (output_file != NULL) { - ioRAW(output_file, false, 0); - fflush(output_file); - // we need to pad to a multiple of 512 bytes for VMS compatibility - fseek(output_file, 0, SEEK_END); - long pos = ftell(output_file); - if (pos % 512 > 0) { - int npad = 512 - pos % 512; - fwrite(zero_pad, 1, npad, output_file); - } - fclose(output_file); - return 0; - } else { - return -1; - } -} - /// stuff int ISISRAW::printInfo(std::ostream &os) { int i; diff --git a/Framework/DataHandling/src/LoadRaw/isisraw.h b/Framework/DataHandling/src/LoadRaw/isisraw.h index c7c419aac95c38cfaaefb20c7d6f262499901955..84d82a06fe282a491b652c79e9f084d907683ecc 100644 --- a/Framework/DataHandling/src/LoadRaw/isisraw.h +++ b/Framework/DataHandling/src/LoadRaw/isisraw.h @@ -373,7 +373,6 @@ public: int ioRAW(FILE *file, DDES_STRUCT **s, int len, bool from_file); int ioRAW(FILE *file, LOG_LINE **s, int len, bool from_file); int readFromFile(const char *filename, bool read_data = true); - int writeToFile(const char *filename); int printInfo(std::ostream &os); int getTimeChannels(float *rtcb1, int n); }; diff --git a/Framework/DataHandling/src/LoadRaw/isisraw2.cpp b/Framework/DataHandling/src/LoadRaw/isisraw2.cpp index f4bbb1f41873c6d09032e9d21c667cd829995d36..331b26bb1f8bb3a6758834da87dc064b47ac0d5a 100644 --- a/Framework/DataHandling/src/LoadRaw/isisraw2.cpp +++ b/Framework/DataHandling/src/LoadRaw/isisraw2.cpp @@ -95,7 +95,17 @@ int ISISRAW2::ioRAW(FILE *file, bool from_file, bool read_data) { // ISISRAW::ioRAW(file, &u_len, 1, from_file); if (from_file) { u_len = add.ad_data - add.ad_user - 2; + + if (u_len < 0 || (add.ad_data < add.ad_user + 2)) { + // this will/would be used for memory allocation + g_log.error() << "Error in u_len value read from file, it would be " + << u_len << "; where it is calculated as " + "u_len = ad_data - ad_user - 2, where ad_data: " + << add.ad_data << ", ad_user: " << add.ad_user << "\n"; + throw std::runtime_error("Inconsistent value for the field u_len found"); + } } + ISISRAW::ioRAW(file, &u_dat, u_len, from_file); ISISRAW::ioRAW(file, &ver8, 1, from_file); fgetpos(file, &dhdr_pos); @@ -115,8 +125,13 @@ int ISISRAW2::ioRAW(FILE *file, bool from_file, bool read_data) { /// @param file :: The file pointer /// @param i :: The amount of data to skip void ISISRAW2::skipData(FILE *file, int i) { - if (i < ndes) - fseek(file, 4 * ddes[i].nwords, SEEK_CUR); + if (i < ndes) { + int zero = fseek(file, 4 * ddes[i].nwords, SEEK_CUR); + if (0 != zero) { + g_log.warning() << "Failed to skip data from file, with value: " << i + << "\n"; + } + } } /// Read data diff --git a/Framework/DataHandling/test/CheckMantidVersionTest.h b/Framework/DataHandling/test/CheckMantidVersionTest.h index d6c1e17a95dc93acdedd0d93979488ee0e964ce3..d7cf6155fca679d6c730a2f7b7f5e55354adddb4 100644 --- a/Framework/DataHandling/test/CheckMantidVersionTest.h +++ b/Framework/DataHandling/test/CheckMantidVersionTest.h @@ -77,7 +77,8 @@ private: "\"https://api.github.com/users/peterfpeterson/received_events\",\n" " \"type\": \"User\",\n" " \"site_admin\": false\n" - " }"; + " }\n" + "}"; return outputString; } diff --git a/Framework/DataObjects/src/MDHistoWorkspaceIterator.cpp b/Framework/DataObjects/src/MDHistoWorkspaceIterator.cpp index 9965a7d050731a517612282a0975edcaa791e746..87ad5e25ccdc9c2f2b6a49f9bee92cd90d46debd 100644 --- a/Framework/DataObjects/src/MDHistoWorkspaceIterator.cpp +++ b/Framework/DataObjects/src/MDHistoWorkspaceIterator.cpp @@ -199,8 +199,17 @@ void MDHistoWorkspaceIterator::init( for (size_t d = 0; d < m_nd; d++) m_center[d] = m_origin[d] + 0.5f * m_binWidth[d]; // Skip on if the first point is NOT contained - if (!m_function->isPointContained(m_center)) - next(); + if (!m_function->isPointContained(m_center)) { + bool didNext = next(); + if (!didNext && this->valid()) { + throw std::runtime_error( + "Inconsistency found initializing " + "MDHistoWorkspace iterator: this iterator should be valid, but " + "when tried to skip the " + "first point (not contained) could not iterate to " + "next point."); + } + } } // --- Calculate index permutations for neighbour finding face touching --- @@ -293,9 +302,11 @@ bool MDHistoWorkspaceIterator::valid() const { return (m_pos < m_max); } /// @return true if you can continue iterating bool MDHistoWorkspaceIterator::next() { if (m_function) { + bool allIncremented = false; do { m_pos++; - Utils::NestedForLoop::Increment(m_nd, m_index, m_indexMax); + allIncremented = + Utils::NestedForLoop::Increment(m_nd, m_index, m_indexMax); // Calculate the center for (size_t d = 0; d < m_nd; d++) { m_center[d] = @@ -304,7 +315,8 @@ bool MDHistoWorkspaceIterator::next() { } // std::cout<<std::endl; // Keep incrementing until you are in the implicit function - } while (!m_function->isPointContained(m_center) && m_pos < m_max); + } while (!allIncremented && !m_function->isPointContained(m_center) && + m_pos < m_max); } else { ++m_pos; } diff --git a/Framework/DataObjects/src/PeakColumn.cpp b/Framework/DataObjects/src/PeakColumn.cpp index f8ffb4af0964c3be9a10fbfb6884363a31dc4dd4..7554f7f04ab8fa95178afe9f906f3cfffa88caf6 100644 --- a/Framework/DataObjects/src/PeakColumn.cpp +++ b/Framework/DataObjects/src/PeakColumn.cpp @@ -80,7 +80,13 @@ PeakColumn::PeakColumn(std::vector<Peak> &peaks, const std::string &name) this->m_name = name; this->m_type = typeFromName(name); // Throws if the name is unknown this->m_hklPrec = 2; - ConfigService::Instance().getValue("PeakColumn.hklPrec", this->m_hklPrec); + const std::string key = "PeakColumn.hklPrec"; + int gotit = ConfigService::Instance().getValue(key, this->m_hklPrec); + if (!gotit) + g_log.information() + << "In PeakColumn constructor, did not find any value for '" << key + << "' from the Config Service. Using default: " << this->m_hklPrec + << "\n"; } //---------------------------------------------------------------------------------------------- diff --git a/Framework/Geometry/src/Objects/Object.cpp b/Framework/Geometry/src/Objects/Object.cpp index e6ab975dda916bb686a74b0efc4530155844cac6..d7bccf922d68d8ae460d4ef200e57dec7ee257a5 100644 --- a/Framework/Geometry/src/Objects/Object.cpp +++ b/Framework/Geometry/src/Objects/Object.cpp @@ -221,11 +221,13 @@ int Object::complementaryObject(const int Cnum, std::string &Ln) { std::string::size_type posB; posB = Ln.find_first_of("()", posA); if (posB == std::string::npos) - throw std::runtime_error("Object::complemenet :: " + Ln); + throw std::runtime_error("Object::complement :: " + Ln); brackCnt = (Ln[posB] == '(') ? 1 : 0; while (posB != std::string::npos && brackCnt) { posB = Ln.find_first_of("()", posB); + if (posB == std::string::npos) + break; brackCnt += (Ln[posB] == '(') ? 1 : -1; posB++; } @@ -242,7 +244,7 @@ int Object::complementaryObject(const int Cnum, std::string &Ln) { return 1; } - throw std::runtime_error("Object::complemenet :: " + Part); + throw std::runtime_error("Object::complement :: " + Part); return 0; } diff --git a/Framework/ICat/src/GSoap/stdsoap2.cpp b/Framework/ICat/src/GSoap/stdsoap2.cpp index 7a966fb7a437dd7c1b35655b223446dc0506e1f8..252bc7ae6514f1f8380bd35f9c89fcc5e46abd54 100644 --- a/Framework/ICat/src/GSoap/stdsoap2.cpp +++ b/Framework/ICat/src/GSoap/stdsoap2.cpp @@ -2348,7 +2348,7 @@ int SOAP_FMAC2 soap_resolve(struct soap *soap) { } else if (*ip->id == '#') { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Missing data for id='%s'\n", ip->id)); - strcpy(soap->id, ip->id + 1); + strncpy(soap->id, ip->id + 1, sizeof(soap->id)); return soap->error = SOAP_MISSING_ID; } } @@ -3207,7 +3207,7 @@ const char *SOAP_FMAC2 soap_ssl_error(struct soap *soap, int ret) { int err = SSL_get_error(soap->ssl, ret); const char *msg = soap_code_str(h_ssl_error_codes, err); if (msg) - strcpy(soap->msgbuf, msg); + strncpy(soap->msgbuf, msg, sizeof(soap->msgbuf)); else return ERR_error_string(err, soap->msgbuf); if (ERR_peek_error()) { @@ -6049,7 +6049,7 @@ static int http_post(struct soap *soap, const char *endpoint, const char *host, sprintf(soap->tmpbuf, "[%s]", host); /* RFC 2732 */ else #endif - strcpy(soap->tmpbuf, host); + strncpy(soap->tmpbuf, host, sizeof(soap->tmpbuf)); } if ((err = soap->fposthdr(soap, "Host", soap->tmpbuf))) return err; @@ -8161,7 +8161,7 @@ void *SOAP_FMAC2 soap_id_lookup(struct soap *soap, const char *id, void **p, "Resolved href='%s' type=%d location=%p (%u bytes)\n", id, t, ip->ptr, (unsigned int)n)); if (ip->type != t) { - strcpy(soap->id, id); + strncpy(soap->id, id, sizeof(soap->id)); soap->error = SOAP_HREF; DBGLOG(TEST, SOAP_MESSAGE( @@ -8265,7 +8265,7 @@ void *SOAP_FMAC2 soap_id_forward(struct soap *soap, const char *href, void *p, "size=%lu level=%u got type=%d size=%lu\n", href, ip->type, (unsigned long)ip->size, k, st, (unsigned long)n)); - strcpy(soap->id, href); + strncpy(soap->id, href, sizeof(soap->id)); soap->error = SOAP_HREF; return NULL; } @@ -8357,12 +8357,12 @@ soap_id_enter(struct soap *soap, const char *id, void *p, int t, size_t n, "size=%lu level=%u got type=%d size=%lu\n", id, ip->type, (unsigned long)ip->size, k, t, (unsigned long)n)); - strcpy(soap->id, id); + strncpy(soap->id, id, sizeof(soap->id)); soap->error = SOAP_HREF; return NULL; } else if (ip->ptr) { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Multiply defined id='%s'\n", id)); - strcpy(soap->id, id); + strncpy(soap->id, id, sizeof(soap->id)); soap->error = SOAP_DUPLICATE_ID; return NULL; } else { @@ -16062,18 +16062,24 @@ int SOAP_FMAC2 soap_puthttphdr(struct soap *soap, int status, size_t count) { } else strcat(soap->tmpbuf, s); if (soap->mime.start) { - strcat(soap->tmpbuf, "\"; start=\""); - strcat(soap->tmpbuf, soap->mime.start); + const char startStr[] = "\"; start=\""; + strcat(soap->tmpbuf, startStr); + strncat(soap->tmpbuf, soap->mime.start, + sizeof(soap->tmpbuf) - sizeof(startStr)); } strcat(soap->tmpbuf, "\""); if (r) { - strcat(soap->tmpbuf, "; start-info=\""); - strcat(soap->tmpbuf, r); - strcat(soap->tmpbuf, "\""); + const char startInfoStr[] = "; start-info=\""; + size_t lenStart = sizeof(soap->tmpbuf) - sizeof(startInfoStr); + strncat(soap->tmpbuf, startInfoStr, lenStart); + size_t lenR = lenStart - strnlen(r, lenStart); + strncat(soap->tmpbuf, r, lenR); + size_t lenEnd = lenR - 1; + strncat(soap->tmpbuf, "\"", lenEnd); } s = soap->tmpbuf; } else - s = strcpy(soap->tmpbuf, s); + s = strncpy(soap->tmpbuf, s, sizeof(soap->tmpbuf)); if (status == SOAP_OK && soap->version == 2 && soap->action && strlen(soap->action) + strlen(s) < sizeof(soap->tmpbuf) - 80) sprintf(soap->tmpbuf + strlen(s), "; action=\"%s\"", soap->action); diff --git a/Framework/Kernel/src/ANN/kd_dump.cpp b/Framework/Kernel/src/ANN/kd_dump.cpp index 203a443249f79c5ed1a24aa84dcac9c33ae2d1b0..eb99a37d414ff2aaefaad18ba3f2f46450b7553c 100644 --- a/Framework/Kernel/src/ANN/kd_dump.cpp +++ b/Framework/Kernel/src/ANN/kd_dump.cpp @@ -34,6 +34,8 @@ #include "kd_tree.h" // kd-tree declarations #include "bd_tree.h" // bd-tree declarations +#include <limits> + using namespace std; // make std:: available //---------------------------------------------------------------------- @@ -332,6 +334,15 @@ annReadDump(istream &in, // input stream for (j = 0; j < the_dim; j++) { // read bounding box low in >> the_bnd_box_hi[j]; } + + if (0 > the_n_pts || + static_cast<size_t>(std::numeric_limits<int>::max()) <= + static_cast<size_t>(the_n_pts / sizeof(ANNidx))) { + annError("Too big number of elements for the point index array. This " + "would cause an overflow when allocating memory", + ANNabort); + } + the_pidx = new ANNidx[the_n_pts]; // allocate point index array int next_idx = 0; // number of indices filled // read the tree and indices @@ -429,6 +440,16 @@ static ANNkd_ptr annReadTree(istream &in, // input stream int n_bnds; // number of bounding sides in >> n_bnds; // number of bounding sides // allocate bounds array + + if (0 > n_bnds || + static_cast<size_t>(std::numeric_limits<int>::max()) <= + static_cast<size_t>(n_bnds / sizeof(ANNorthHalfSpace))) { + annError("Too big number of bounding sides, would cause overflow when " + "allocating memory", + ANNabort); + exit(0); + } + ANNorthHSArray bds = new ANNorthHalfSpace[n_bnds]; for (int i = 0; i < n_bnds; i++) { int sd; // which side diff --git a/Framework/LiveData/src/ISISHistoDataListener.cpp b/Framework/LiveData/src/ISISHistoDataListener.cpp index d962ecea3eafff1aabb5550675c27bf898669eba..b211f601a4eb5dd334c87e053d597a875164d35d 100644 --- a/Framework/LiveData/src/ISISHistoDataListener.cpp +++ b/Framework/LiveData/src/ISISHistoDataListener.cpp @@ -181,6 +181,10 @@ boost::shared_ptr<Workspace> ISISHistoDataListener::extractData() { getFloatArray("RRPB", floatBuffer, 32); const double protonCharge = floatBuffer[8]; + if (m_timeRegime < 0) + throw std::runtime_error("The value of the time regime variable is " + "negative. This is an Internal inconsistency."); + // find out the number of histograms in the output workspace const size_t numberOfHistograms = m_specList.empty() ? m_numberOfSpectra[m_timeRegime] : m_specList.size(); diff --git a/Framework/LiveData/src/LoadDAE/isisds_command.cpp b/Framework/LiveData/src/LoadDAE/isisds_command.cpp index 790c2391b29ba8394be8f545f77d2e4e4fa1eeb6..b54ea2c9781699874f8f7b5c3b9b3cc1ba9209b8 100644 --- a/Framework/LiveData/src/LoadDAE/isisds_command.cpp +++ b/Framework/LiveData/src/LoadDAE/isisds_command.cpp @@ -145,8 +145,14 @@ SOCKET isisds_send_open(const char *host, ISISDSAccessMode access_type, if (s == INVALID_SOCKET) { return INVALID_SOCKET; } - setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&setkeepalive, - sizeof(setkeepalive)); + + int zero = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&setkeepalive, + sizeof(setkeepalive)); + if (0 != zero) { + closesocket(s); + return INVALID_SOCKET; + } + if (connect(s, (struct sockaddr *)&address, sizeof(address)) == -1) { closesocket(s); return INVALID_SOCKET; diff --git a/MantidPlot/src/Mantid/InstrumentWidget/OpenGLError.cpp b/MantidPlot/src/Mantid/InstrumentWidget/OpenGLError.cpp index 8da241a02ea906a903b8742b08ca3c812e99e00f..afa7126f7499db695f9a5f731a0b5a23eb26f4bc 100644 --- a/MantidPlot/src/Mantid/InstrumentWidget/OpenGLError.cpp +++ b/MantidPlot/src/Mantid/InstrumentWidget/OpenGLError.cpp @@ -33,3 +33,8 @@ std::ostream& OpenGLError::log() { return g_log.error(); } + +std::ostream& OpenGLError::logDebug() +{ + return g_log.debug(); +} diff --git a/MantidPlot/src/Mantid/InstrumentWidget/OpenGLError.h b/MantidPlot/src/Mantid/InstrumentWidget/OpenGLError.h index cdeb10d98f2ca333a20654e82d640597988998f8..c5e63188a828b6325d5725d70a9a11ba5006b648 100644 --- a/MantidPlot/src/Mantid/InstrumentWidget/OpenGLError.h +++ b/MantidPlot/src/Mantid/InstrumentWidget/OpenGLError.h @@ -16,6 +16,8 @@ public: static bool check(const std::string& funName); static bool hasError(const std::string& funName){return check(funName);} static std::ostream& log(); + static std::ostream& logDebug(); + private: std::string m_msg; }; diff --git a/MantidPlot/src/Mantid/InstrumentWidget/ProjectionSurface.cpp b/MantidPlot/src/Mantid/InstrumentWidget/ProjectionSurface.cpp index 1c6a0052da5fb529510990d2b153790c8ae3dee3..37a85ee1cfdd7f02149a70bff5c8a43c632989ad 100644 --- a/MantidPlot/src/Mantid/InstrumentWidget/ProjectionSurface.cpp +++ b/MantidPlot/src/Mantid/InstrumentWidget/ProjectionSurface.cpp @@ -220,7 +220,8 @@ void ProjectionSurface::draw(MantidGLWidget *widget,bool picking)const getController()->onPaint( painter ); painter.end(); // Discard any error generated here - glGetError(); + GLuint ecode = glGetError(); + OpenGLError::logDebug() << "Discarding OpenGL error: " << gluErrorString(ecode); } } diff --git a/MantidPlot/src/origin/OPJFile.cpp b/MantidPlot/src/origin/OPJFile.cpp index c0eaf257564f31fb9b64b0ffd2f0ac8fb75696b6..73c397f65f2b0677228b8845a3ee1219b7708505 100644 --- a/MantidPlot/src/origin/OPJFile.cpp +++ b/MantidPlot/src/origin/OPJFile.cpp @@ -52,6 +52,7 @@ #include <stdio.h> #include <stdlib.h> +#include <limits.h> #include <math.h> #include <cstring> #include <algorithm> //required for std::swap @@ -1327,6 +1328,12 @@ void OPJFile::readSpreadInfo(FILE *f, FILE *debug) fread(&sec_size,4,1,f); if(IsBigEndian()) SwapBytes(sec_size); + if (INT_MAX == sec_size) { + // this would end in an overflow and it's obviously wrong + fprintf(debug, "Error: while reading spread info, found section size: %d\n", sec_size); + fflush(debug); + } + //section_body_1 LAYER+=0x5; fseek(f,LAYER,SEEK_SET); @@ -1335,6 +1342,9 @@ void OPJFile::readSpreadInfo(FILE *f, FILE *debug) if(col_index!=-1) { char *stmp=new char[sec_size+1]; + if (!stmp) + break; + stmp[sec_size]='\0'; fread(stmp,sec_size,1,f); SPREADSHEET[spread].column[col_index].command=stmp; @@ -1552,6 +1562,12 @@ void OPJFile::readExcelInfo(FILE *f, FILE *debug) fread(&sec_size,4,1,f); if(IsBigEndian()) SwapBytes(sec_size); + if (INT_MAX == sec_size) { + // this would end in an overflow for new[] below and it's obviously wrong + fprintf(debug, "Error: while reading Excel info, found section size: %d\n", sec_size); + fflush(debug); + } + //section_body_1 LAYER+=0x5; fseek(f,LAYER,SEEK_SET); @@ -1802,6 +1818,12 @@ void OPJFile::readMatrixInfo(FILE *f, FILE *debug) fread(&sec_size,4,1,f); if(IsBigEndian()) SwapBytes(sec_size); + if (INT_MAX == sec_size) { + // this would end in an overflow for new[] below and it's obviously wrong + fprintf(debug, "Error: while reading matrix info, found section size: %d\n", sec_size); + fflush(debug); + } + //section_body_1 LAYER+=0x5; //check if it is a formula @@ -2933,11 +2955,17 @@ void OPJFile::readProjectTreeFolder(FILE *f, FILE *debug, tree<projectNode>::ite fread(&namesize,4,1,f); if(IsBigEndian()) SwapBytes(namesize); - POS+=5; + if (INT_MAX == namesize) { + // this would cause an overflow and it's anyway obviously wrong + fprintf(debug, "Error: while reading project tree folder, found project/folder name size: %d\n", namesize); + fflush(debug); + } // read folder name char* name=new char[namesize+1]; name[namesize]='\0'; + + POS+=5; fseek(f,POS,SEEK_SET); fread(name,namesize,1,f); tree<projectNode>::iterator current_folder=projectTree.append_child(parent, projectNode(name, 1, creation_date, modification_date)); diff --git a/MantidPlot/src/zlib123/minigzip.c b/MantidPlot/src/zlib123/minigzip.c index 6f12762c8ecedb7a71ddf33c3f7f848178ba3075..5263508908db17952ab8df54cbd0d1bfc0575657 100644 --- a/MantidPlot/src/zlib123/minigzip.c +++ b/MantidPlot/src/zlib123/minigzip.c @@ -209,8 +209,9 @@ void file_compress(file, mode) FILE *in; gzFile out; - strcpy(outfile, file); - strcat(outfile, GZ_SUFFIX); + strncpy(outfile, file, MAX_NAME_LEN-1); + outfile[MAX_NAME_LEN-1] = '\0'; + strncat(outfile, GZ_SUFFIX, MAX_NAME_LEN - strlen(outfile) - 1); in = fopen(file, "rb"); if (in == NULL) {