Jordan Borisov Jordan Borisov - 3 months ago 13
C++ Question

Program error ntdll!RtlQueryPerformanceCounter()

When I run this code :

//============================================================================
// Name : sum.cpp
// Author : Yordan Borisov
// Version :
// Copyright : MIT License
//
// Description : Sum bug numbers in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int _INT(char ch) {
return ch - '0';
}
char _CHAR(int n) {
return n + '0';
}

char* validateSize(int size_one, int size_two, char* _n_T, char*& _n_O) {
if (size_one != size_two) {
int diff = abs(size_one - size_two);
int _less_Length = strlen((size_one > size_two ? _n_T : _n_O));
char* tmp = new char[_less_Length + diff];
strcpy(tmp, "\0");
int _index = 0;
for (int i = 0; i < diff; i++) {
tmp[_index++] = '0';
}
tmp[_index] = '\0';
for (int i = 0; i < _less_Length; i++) {
tmp[_index++] = (size_one > size_two ? _n_T : _n_O)[i];
}
tmp[_index] = '\0';
(size_one > size_two ? _n_T : _n_O) = tmp;
}
return _n_T;
}

char* sum(const char* numOne, const char* numTwo) {
int size_one = strlen(numOne);
int size_two = strlen(numTwo);
char* _n_O = new char[size_one];
char* _n_T = new char[size_two];
strcpy(_n_O, numOne);
strcpy(_n_T, numTwo);
_n_T = validateSize(size_one, size_two, _n_T, _n_O);
char* result = new char[size_one + 1];
result[size_one] = '\0';
int _CF = 0;
int _increase = 0;
for (long i = (size_one > size_two ? size_one : size_two) - 1; i >= 0; i--) {
char chOne = _n_O[i];
char chTwo = _n_T[i];
int _sum = _INT(chOne) + _INT(chTwo) + _CF;
_CF = _sum / 10;
result[i] = _CHAR(_sum % 10);
if (i == 0 && _CF > 0) {
//in that case additional element in the result must be added.
char _ch = _CHAR(_CF);
char* _tmp_Pr = new char[1];
_tmp_Pr[0] = _ch;
_tmp_Pr[1] = '\0';
_increase = 1;
char* _tmp_R = strcat(_tmp_Pr, result);
strcpy(result, _tmp_R);
}
}
result[(size_one > size_two ? size_one : size_two) + _increase] = '\0';
return result;
}
int main(int argc, char* argv[]) {

cout
<< sum(
"147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522211478523693147852369365215486695222146521548221446521548221414785236931478523693652154866952221465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222114785236931478523693652154866952221465215482214465215482214147852369314785236936521548669522214652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221147852369314785236936521548669522214652154822144652154822141478523693147852369365215486695222146521548221414785236931478523693652154866952221465215482214",
"3652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144365215463284452116500052856321822136365215463284452116500052856321822145215463284452116500052856321822143652154632844521165000528563218221436521546328445211650005285632182214436521546328445211650005285632182213636521546328445211650005285632182214521546328445211650005285632182214365215463284452116500052856321822143652154632844521165000528563218221443652154632844521165000528563218221363652154632844521165000528563218221452154632844521165000528563218221436521546328445211650005285632182214365215463284452116500052856321822144")
<< endl;
return 0;
}


I've got this exception:

Thread #1 0 (Suspended : Signal : SIGSEGV:Segmentation fault)
ntdll!RtlQueryPerformanceCounter() at 0x777531f6
ntdll!RtlQueryPerformanceCounter() at 0x77753431
ntdll!RtlpNtEnumerateSubKey() at 0x777f0f0e
ntdll!RtlUlonglongByteSwap() at 0x777aaa86
0x740000
ntdll!RtlQueryPerformanceCounter() at 0x77753431
towlower() at 0x7700ade8
towlower() at 0x7700ae43
msvcrt!_close() at 0x77013bb9
0x1
<...more frames...>


In the disassembly code on line 777764bc:

......... ...
777531c2: dec %esp
777531c3: add %dh,-0x75(%esi,%edx,1)
777531c7: dec %eax
777531c8: push %eax
777531c9: xor %ecx,(%esi)
777531cb: mov 0x2(%esi),%cl
777531ce: xor 0x1(%esi),%cl
777531d1: xor (%esi),%cl
777531d3: cmp %cl,0x3(%esi)
777531d6: jne 0x777aab1a <ntdll!RtlUlonglongByteSwap+56842>
777531dc: movzwl (%esi),%ebx
777531df: cmp -0x2c(%ebp),%ebx
777531e2: jb 0x77794568 <ntdll!RtlGetProcessHeaps+199>
777531e8: lea 0x8(%esi),%ecx
777531eb: mov (%ecx),%edi
777531ed: mov %edi,-0x48(%ebp)
777531f0: mov 0xc(%esi),%edx
777531f3: mov %edx,-0x68(%ebp)
777531f6: mov (%edx),%edx
777531f8: mov 0x4(%edi),%edi
777531fb: cmp %edi,%edx
777531fd: jne 0x7779455b <ntdll!RtlGetProcessHeaps+186>
77753203: cmp %ecx,%edx
77753205: jne 0x7779455b <ntdll!RtlGetProcessHeaps+186>
7775320b: sub %ebx,0x78(%eax)
7775320e: mov 0xb8(%eax),%eax
77753214: test %eax,%eax
77753216: je 0x777532ed <ntdll!RtlQueryPerformanceCounter+2457>
7775321c: movzwl (%esi),%edi
7775321f: mov %eax,-0xb4(%ebp)
77753225: mov 0x4(%eax),%ecx
77753228: cmp %ecx,%edi
7775322a: jae 0x777537b3 <ntdll!RtlQueryPerformanceCounter+3679>
77753230: mov %edi,-0x8c(%ebp)
77753236: mov -0x8c(%ebp),%ecx
7775323c: sub 0x14(%eax),%ecx
7775323f: mov %ecx,0x10(%ebp)
77753242: cmpl $0x0,0x8(%eax)
77753246: je 0x7775324a <ntdll!RtlQueryPerformanceCounter+2294>
77753248: add %ecx,%ecx
7775324a: mov %ecx,%edi
7775324c: shl $0x2,%edi
7775324f: mov 0x20(%eax),%ecx
77753252: lea (%edi,%ecx,1),%edx
77753255: mov (%edx),%ecx
77753257: mov %ecx,-0x38(%ebp)
7775325a: decl 0xc(%eax)
7775325d: mov 0x4(%eax),%ecx
77753260: dec %ecx
77753261: cmp %ecx,-0x8c(%ebp)
......... ...

Answer

So I fixed the problem : it was in the

char* result = new char[(size_one > size_two ? size_one : size_two) + 1];
result[(size_one > size_two ? size_one : size_two)] = '\0';

and I was using :

char* result = new char[size_one + 1];
result[size_one] = '\0';

which apperantly is wrong because the size_one is smaller that size_two.