@@ -231,9 +231,9 @@ check_buf_append(
231231 git_buf_puts (& tgt , data_b );
232232 cl_assert (git_buf_oom (& tgt ) == 0 );
233233 cl_assert_equal_s (expected_data , git_buf_cstr (& tgt ));
234- cl_assert (tgt .size == expected_size );
234+ cl_assert_equal_i (tgt .size , expected_size );
235235 if (expected_asize > 0 )
236- cl_assert (tgt .asize == expected_asize );
236+ cl_assert_equal_i (tgt .asize , expected_asize );
237237
238238 git_buf_dispose (& tgt );
239239}
@@ -308,7 +308,7 @@ void test_core_buffer__5(void)
308308 REP16 ("x" ) REP16 ("o" ), 32 , 40 );
309309
310310 check_buf_append (test_4096 , "" , test_4096 , 4096 , 4104 );
311- check_buf_append (test_4096 , test_4096 , test_8192 , 8192 , 9240 );
311+ check_buf_append (test_4096 , test_4096 , test_8192 , 8192 , 8200 );
312312
313313 /* check sequences of appends */
314314 check_buf_append_abc ("a" , "b" , "c" ,
@@ -1204,3 +1204,39 @@ void test_core_buffer__dont_grow_borrowed(void)
12041204
12051205 cl_git_fail_with (GIT_EINVALID , git_buf_grow (& buf , 1024 ));
12061206}
1207+
1208+ void test_core_buffer__dont_hit_infinite_loop_when_resizing (void )
1209+ {
1210+ git_buf buf = GIT_BUF_INIT ;
1211+
1212+ cl_git_pass (git_buf_puts (& buf , "foobar" ));
1213+ /*
1214+ * We do not care whether this succeeds or fails, which
1215+ * would depend on platform-specific allocation
1216+ * semantics. We only want to know that the function
1217+ * actually returns.
1218+ */
1219+ (void )git_buf_try_grow (& buf , SIZE_MAX , true);
1220+
1221+ git_buf_dispose (& buf );
1222+ }
1223+
1224+ void test_core_buffer__avoid_printing_into_oom_buffer (void )
1225+ {
1226+ git_buf buf = GIT_BUF_INIT ;
1227+
1228+ /* Emulate OOM situation with a previous allocation */
1229+ buf .asize = 8 ;
1230+ buf .ptr = git_buf__oom ;
1231+
1232+ /*
1233+ * Print the same string again. As the buffer still has
1234+ * an `asize` of 8 due to the previous print,
1235+ * `ENSURE_SIZE` would not try to reallocate the array at
1236+ * all. As it didn't explicitly check for `git_buf__oom`
1237+ * in earlier versions, this would've resulted in it
1238+ * returning successfully and thus `git_buf_puts` would
1239+ * just print into the `git_buf__oom` array.
1240+ */
1241+ cl_git_fail (git_buf_puts (& buf , "foobar" ));
1242+ }
0 commit comments