@@ -35,36 +35,51 @@ void TTDHostInitFromUriBytes(TTDHostCharType* dst, const byte* uriBytes, size_t
35
35
AssertMsg (wcslen (dst) == (uriBytesLength / sizeof (TTDHostCharType)), " We have an null in the uri or our math is wrong somewhere." );
36
36
}
37
37
38
- void TTDHostAppend (TTDHostCharType* dst, const TTDHostCharType* src)
38
+ void TTDHostAppend (TTDHostCharType* dst, size_t dstLength, const TTDHostCharType* src)
39
39
{
40
- size_t dpos = TTDHostStringLength (dst);
41
40
size_t srcLength = TTDHostStringLength (src);
41
+ size_t dpos = TTDHostStringLength (dst);
42
+ Helpers::TTReportLastIOErrorAsNeeded (dpos < dstLength, " The end of the string already exceeds the buffer" );
43
+
42
44
size_t srcByteLength = srcLength * sizeof (TTDHostCharType);
45
+ size_t dstRemainingByteLength = (dstLength - dpos - 1 ) * sizeof (TTDHostCharType);
46
+ Helpers::TTReportLastIOErrorAsNeeded (srcByteLength <= dstRemainingByteLength, " The source string must be able to fit within the destination buffer" );
43
47
44
- memcpy_s (dst + dpos, srcByteLength , src, srcByteLength);
48
+ memcpy_s (dst + dpos, dstRemainingByteLength , src, srcByteLength);
45
49
dst[dpos + srcLength] = _u (' \0 ' );
46
50
}
47
51
48
- void TTDHostAppendWChar (TTDHostCharType* dst, const wchar* src)
52
+ void TTDHostAppendWChar (TTDHostCharType* dst, size_t dstLength, const wchar* src)
49
53
{
50
- size_t dpos = TTDHostStringLength (dst);
51
54
size_t srcLength = wcslen (src);
55
+ size_t dpos = TTDHostStringLength (dst);
56
+ Helpers::TTReportLastIOErrorAsNeeded (dpos < dstLength, " The end of the string already exceeds the buffer" );
57
+
58
+ size_t dstRemainingLength = dstLength - dpos - 1 ;
59
+ Helpers::TTReportLastIOErrorAsNeeded (srcLength <= dstRemainingLength, " The source string must be able to fit within the destination buffer" );
52
60
53
61
for (size_t i = 0 ; i < srcLength; ++i)
54
62
{
55
63
dst[dpos + i] = (char16)src[i];
56
64
}
65
+
57
66
dst[dpos + srcLength] = _u (' \0 ' );
58
67
}
59
68
60
- void TTDHostAppendAscii (TTDHostCharType* dst, const char * src)
69
+ void TTDHostAppendAscii (TTDHostCharType* dst, size_t dstLength, const char * src)
61
70
{
62
- size_t dpos = TTDHostStringLength (dst);
63
71
size_t srcLength = strlen (src);
72
+ size_t dpos = TTDHostStringLength (dst);
73
+ Helpers::TTReportLastIOErrorAsNeeded (dpos < dstLength, " The end of the string already exceeds the buffer" );
74
+
75
+ size_t dstRemainingLength = dstLength - dpos - 1 ;
76
+ Helpers::TTReportLastIOErrorAsNeeded (srcLength <= dstRemainingLength, " The source string must be able to fit within the destination buffer" );
77
+
64
78
for (size_t i = 0 ; i < srcLength; ++i)
65
79
{
66
80
dst[dpos + i] = (char16)src[i];
67
81
}
82
+
68
83
dst[dpos + srcLength] = _u (' \0 ' );
69
84
}
70
85
@@ -80,7 +95,7 @@ void TTDHostBuildCurrentExeDirectory(TTDHostCharType* path, size_t pathBufferLen
80
95
}
81
96
exePath[i + 1 ] = _u (' \0 ' );
82
97
83
- TTDHostAppendWChar (path, exePath);
98
+ TTDHostAppendWChar (path, pathBufferLength, exePath);
84
99
}
85
100
86
101
JsTTDStreamHandle TTDHostOpen (const TTDHostCharType* path, bool isWrite)
@@ -91,8 +106,8 @@ JsTTDStreamHandle TTDHostOpen(const TTDHostCharType* path, bool isWrite)
91
106
return (JsTTDStreamHandle)res;
92
107
}
93
108
94
- #define TTDHostCWD (dst ) _wgetcwd(dst, MAX_PATH )
95
- #define TTDDoPathInit (dst )
109
+ #define TTDHostCWD (dst, dstLength ) _wgetcwd(dst, dstLength )
110
+ #define TTDDoPathInit (dst, dstLength )
96
111
#define TTDHostTok (opath, TTDHostPathSeparator, context ) wcstok_s(opath, TTDHostPathSeparator, context)
97
112
#define TTDHostStat (cpath, statVal ) _wstat(cpath, statVal)
98
113
@@ -148,30 +163,44 @@ void TTDHostInitFromUriBytes(TTDHostCharType* dst, const byte* uriBytes, size_t
148
163
AssertMsg (TTDHostStringLength (dst) == (uriBytesLength / sizeof (TTDHostCharType)), " We have an null in the uri or our math is wrong somewhere." );
149
164
}
150
165
151
- void TTDHostAppend (TTDHostCharType* dst, const TTDHostCharType* src)
166
+ void TTDHostAppend (TTDHostCharType* dst, size_t dstLength, const TTDHostCharType* src)
152
167
{
153
- size_t dpos = TTDHostStringLength (dst);
154
168
size_t srcLength = TTDHostStringLength (src);
169
+ size_t dpos = TTDHostStringLength (dst);
170
+ Helpers::TTReportLastIOErrorAsNeeded (dpos < dstLength, " The end of the string already exceeds the buffer" );
171
+
155
172
size_t srcByteLength = srcLength * sizeof (TTDHostCharType);
173
+ size_t dstRemainingByteLength = (dstLength - dpos - 1 ) * sizeof (TTDHostCharType);
174
+ Helpers::TTReportLastIOErrorAsNeeded (srcByteLength <= dstRemainingByteLength, " The source string must be able to fit within the destination buffer" );
156
175
157
- memcpy_s (dst + dpos, srcByteLength , src, srcByteLength);
176
+ memcpy_s (dst + dpos, dstRemainingByteLength , src, srcByteLength);
158
177
dst[dpos + srcLength] = ' \0 ' ;
159
178
}
160
179
161
- void TTDHostAppendWChar (TTDHostCharType* dst, const wchar* src)
180
+ void TTDHostAppendWChar (TTDHostCharType* dst, size_t dstLength, const wchar* src)
162
181
{
163
- size_t dpos = TTDHostStringLength (dst);
164
182
size_t srcLength = wcslen (src);
183
+ size_t dpos = TTDHostStringLength (dst);
184
+ Helpers::TTReportLastIOErrorAsNeeded (dpos < dstLength, " The end of the string already exceeds the buffer" );
185
+
186
+ size_t dstRemainingLength = dstLength - dpos - 1 ;
187
+ Helpers::TTReportLastIOErrorAsNeeded (srcLength <= dstRemainingLength, " The source string must be able to fit within the destination buffer" );
188
+
189
+ // TODO - analyze this function further
165
190
utf8::EncodeIntoAndNullTerminate (dst + dpos, src, srcLength);
166
191
}
167
192
168
- void TTDHostAppendAscii (TTDHostCharType* dst, const char * src)
193
+ void TTDHostAppendAscii (TTDHostCharType* dst, size_t dstLength, const char * src)
169
194
{
170
- size_t dpos = TTDHostStringLength (dst);
171
195
size_t srcLength = strlen (src);
196
+ size_t dpos = TTDHostStringLength (dst);
197
+ Helpers::TTReportLastIOErrorAsNeeded (dpos < dstLength, " The end of the string already exceeds the buffer" );
198
+
172
199
size_t srcByteLength = srcLength * sizeof (TTDHostCharType);
200
+ size_t dstRemainingByteLength = (dstLength - dpos - 1 ) * sizeof (TTDHostCharType);
201
+ Helpers::TTReportLastIOErrorAsNeeded (srcByteLength <= dstRemainingByteLength, " The source string must be able to fit within the destination buffer" );
173
202
174
- memcpy_s (dst + dpos, srcByteLength , src, srcByteLength);
203
+ memcpy_s (dst + dpos, dstRemainingByteLength , src, srcByteLength);
175
204
dst[dpos + srcLength] = ' \0 ' ;
176
205
}
177
206
@@ -192,18 +221,19 @@ void TTDHostBuildCurrentExeDirectory(TTDHostCharType* path, size_t pathBufferLen
192
221
{
193
222
--i;
194
223
}
224
+
195
225
exePath[i + 1 ] = ' \0 ' ;
196
226
197
- TTDHostAppend (path, exePath);
227
+ TTDHostAppend (path, pathBufferLength, exePath);
198
228
}
199
229
200
230
JsTTDStreamHandle TTDHostOpen (const TTDHostCharType* path, bool isWrite)
201
231
{
202
232
return (JsTTDStreamHandle)fopen (TTDHostCharConvert (path), isWrite ? " w+b" : " r+b" );
203
233
}
204
234
205
- #define TTDHostCWD (dst ) TTDHostUtf8CharConvert(getcwd(TTDHostCharConvert(dst), MAX_PATH ))
206
- #define TTDDoPathInit (dst ) TTDHostAppend(dst, TTDHostPathSeparator)
235
+ #define TTDHostCWD (dst, dstLength ) TTDHostUtf8CharConvert(getcwd(TTDHostCharConvert(dst), dstLength ))
236
+ #define TTDDoPathInit (dst, dstLength ) TTDHostAppend(dst, dstLength , TTDHostPathSeparator)
207
237
#define TTDHostTok (opath, TTDHostPathSeparator, context ) TTDHostUtf8CharConvert(strtok(TTDHostCharConvert(opath), TTDHostCharConvert(TTDHostPathSeparator)))
208
238
#define TTDHostStat (cpath, statVal ) stat(TTDHostCharConvert(cpath), statVal)
209
239
@@ -504,6 +534,7 @@ HRESULT Helpers::LoadBinaryFile(LPCSTR filename, LPCSTR& contents, UINT& lengthB
504
534
505
535
return hr;
506
536
}
537
+
507
538
void Helpers::TTReportLastIOErrorAsNeeded (BOOL ok, const char * msg)
508
539
{
509
540
if (!ok)
@@ -530,19 +561,19 @@ void Helpers::CreateDirectoryIfNeeded(size_t uriByteLength, const byte* uriBytes
530
561
531
562
TTDHostCharType cpath[MAX_PATH];
532
563
TTDHostInitEmpty (cpath);
533
- TTDDoPathInit (cpath);
564
+ TTDDoPathInit (cpath, MAX_PATH );
534
565
535
566
TTDHostStatType statVal;
536
567
TTDHostCharType* context = nullptr ;
537
568
TTDHostCharType* token = TTDHostTok (opath, TTDHostPathSeparator, &context);
538
- TTDHostAppend (cpath, token);
569
+ TTDHostAppend (cpath, MAX_PATH, token);
539
570
540
571
// At least 1 part of the path must exist so iterate until we find it
541
572
while (TTDHostStat (cpath, &statVal) == -1 )
542
573
{
543
574
token = TTDHostTok (nullptr , TTDHostPathSeparator, &context);
544
- TTDHostAppend (cpath, TTDHostPathSeparator);
545
- TTDHostAppend (cpath, token);
575
+ TTDHostAppend (cpath, MAX_PATH, TTDHostPathSeparator);
576
+ TTDHostAppend (cpath, MAX_PATH, token);
546
577
}
547
578
548
579
// Now continue until we hit the part that doesn't exist (or the end of the path)
@@ -551,8 +582,8 @@ void Helpers::CreateDirectoryIfNeeded(size_t uriByteLength, const byte* uriBytes
551
582
token = TTDHostTok (nullptr , TTDHostPathSeparator, &context);
552
583
if (token != nullptr )
553
584
{
554
- TTDHostAppend (cpath, TTDHostPathSeparator);
555
- TTDHostAppend (cpath, token);
585
+ TTDHostAppend (cpath, MAX_PATH, TTDHostPathSeparator);
586
+ TTDHostAppend (cpath, MAX_PATH, token);
556
587
}
557
588
}
558
589
@@ -569,8 +600,8 @@ void Helpers::CreateDirectoryIfNeeded(size_t uriByteLength, const byte* uriBytes
569
600
token = TTDHostTok (nullptr , TTDHostPathSeparator, &context);
570
601
if (token != nullptr )
571
602
{
572
- TTDHostAppend (cpath, TTDHostPathSeparator);
573
- TTDHostAppend (cpath, token);
603
+ TTDHostAppend (cpath, MAX_PATH, TTDHostPathSeparator);
604
+ TTDHostAppend (cpath, MAX_PATH, token);
574
605
}
575
606
}
576
607
}
@@ -582,7 +613,7 @@ void Helpers::CleanDirectory(size_t uriByteLength, const byte* uriBytes)
582
613
583
614
TTDHostCharType strPattern[MAX_PATH];
584
615
TTDHostInitFromUriBytes (strPattern, uriBytes, uriByteLength);
585
- TTDHostAppendAscii (strPattern, " *.*" );
616
+ TTDHostAppendAscii (strPattern, MAX_PATH, " *.*" );
586
617
587
618
hFile = TTDHostFindFirst (strPattern, &FileInformation);
588
619
if (hFile != TTDHostFindInvalid)
@@ -593,7 +624,7 @@ void Helpers::CleanDirectory(size_t uriByteLength, const byte* uriBytes)
593
624
{
594
625
TTDHostCharType strFilePath[MAX_PATH];
595
626
TTDHostInitFromUriBytes (strFilePath, uriBytes, uriByteLength);
596
- TTDHostAppend (strFilePath, TTDHostDirInfoName (FileInformation));
627
+ TTDHostAppend (strFilePath, MAX_PATH, TTDHostDirInfoName (FileInformation));
597
628
598
629
// Set file attributes
599
630
int statusch = TTDHostCHMod (strFilePath, S_IREAD | S_IWRITE);
@@ -616,27 +647,27 @@ void Helpers::GetTTDDirectory(const wchar* curi, size_t* uriByteLength, byte* ur
616
647
617
648
if (curi[0 ] != _u (' ~' ))
618
649
{
619
- TTDHostCharType* status = TTDHostCWD (turi);
650
+ TTDHostCharType* status = TTDHostCWD (turi, MAX_PATH );
620
651
Helpers::TTReportLastIOErrorAsNeeded (status != nullptr , " Failed to chmod directory" );
621
652
622
- TTDHostAppend (turi, TTDHostPathSeparator);
653
+ TTDHostAppend (turi, MAX_PATH, TTDHostPathSeparator);
623
654
624
- TTDHostAppendWChar (turi, curi);
655
+ TTDHostAppendWChar (turi, MAX_PATH, curi);
625
656
}
626
657
else
627
658
{
628
659
TTDHostBuildCurrentExeDirectory (turi, MAX_PATH);
629
660
630
- TTDHostAppendAscii (turi, " _ttdlog" );
631
- TTDHostAppend (turi, TTDHostPathSeparator);
661
+ TTDHostAppendAscii (turi, MAX_PATH, " _ttdlog" );
662
+ TTDHostAppend (turi, MAX_PATH, TTDHostPathSeparator);
632
663
633
- TTDHostAppendWChar (turi, curi + 1 );
664
+ TTDHostAppendWChar (turi, MAX_PATH, curi + 1 );
634
665
}
635
666
636
667
// add a path separator if one is not already present
637
668
if (curi[wcslen (curi) - 1 ] != (wchar)TTDHostPathSeparatorChar)
638
669
{
639
- TTDHostAppend (turi, TTDHostPathSeparator);
670
+ TTDHostAppend (turi, MAX_PATH, TTDHostPathSeparator);
640
671
}
641
672
642
673
size_t turiLength = TTDHostStringLength (turi);
@@ -665,7 +696,7 @@ JsTTDStreamHandle CALLBACK Helpers::TTCreateStreamCallback(size_t uriByteLength,
665
696
void * res = nullptr ;
666
697
TTDHostCharType path[MAX_PATH];
667
698
TTDHostInitFromUriBytes (path, uriBytes, uriByteLength);
668
- TTDHostAppendAscii (path, asciiResourceName);
699
+ TTDHostAppendAscii (path, MAX_PATH, asciiResourceName);
669
700
670
701
res = TTDHostOpen (path, write);
671
702
if (res == nullptr )
0 commit comments