|  | 
| 23 | 23 |    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 
| 24 | 24 |    SOFTWARE. | 
| 25 | 25 | */ | 
| 26 |  | -#define K8_VERSION "1.0-r121" | 
|  | 26 | +#define K8_VERSION "1.0-r124" | 
| 27 | 27 | 
 | 
| 28 | 28 | #include <stdlib.h> | 
| 29 | 29 | #include <stdint.h> | 
| @@ -358,20 +358,22 @@ static void k8_write_string(FILE *fp, const v8::FunctionCallbackInfo<v8::Value> | 
| 358 | 358 | 	v8::HandleScope handle_scope(args.GetIsolate()); | 
| 359 | 359 | 	if (args[i]->IsString()) { | 
| 360 | 360 | 		int64_t len = args[i].As<v8::String>()->Length(); | 
| 361 |  | -		K8_GROW(uint8_t, buf->s, len-1, buf->m); | 
| 362 |  | -		args[i].As<v8::String>()->WriteOneByte(args.GetIsolate(), buf->s); | 
| 363 |  | -		fwrite(buf->s, 1, len, fp); | 
|  | 361 | +		if (len > 0) { | 
|  | 362 | +			K8_GROW(uint8_t, buf->s, len, buf->m); | 
|  | 363 | +			args[i].As<v8::String>()->WriteOneByte(args.GetIsolate(), buf->s); | 
|  | 364 | +			fwrite(buf->s, 1, len, fp); | 
|  | 365 | +		} | 
| 364 | 366 | 		return; | 
| 365 | 367 | 	} else if (args[i]->IsArrayBuffer()) { | 
| 366 | 368 | 		void *data = args[i].As<v8::ArrayBuffer>()->GetBackingStore()->Data(); | 
| 367 | 369 | 		int64_t len = args[i].As<v8::ArrayBuffer>()->GetBackingStore()->ByteLength(); | 
| 368 |  | -		fwrite(data, 1, len, fp); | 
|  | 370 | +		if (len > 0) fwrite(data, 1, len, fp); | 
| 369 | 371 | 		return; | 
| 370 | 372 | 	} else if (args[i]->IsObject()) { | 
| 371 | 373 | 		if (args[i].As<v8::Object>()->InternalFieldCount() > 0) { | 
| 372 | 374 | 			k8_bytes_t *a = (k8_bytes_t*)args[i].As<v8::Object>()->GetAlignedPointerFromInternalField(0); | 
| 373 | 375 | 			if (a && a->magic == K8_BYTES_MAGIC) { | 
| 374 |  | -				fwrite(a->buf.s, 1, a->buf.l, fp); | 
|  | 376 | +				if (a->buf.l > 0) fwrite(a->buf.s, 1, a->buf.l, fp); | 
| 375 | 377 | 				return; | 
| 376 | 378 | 			} | 
| 377 | 379 | 		} | 
| @@ -706,14 +708,14 @@ static void k8_file_write(const v8::FunctionCallbackInfo<v8::Value> &args) | 
| 706 | 708 | 		void *data = args[0].As<v8::ArrayBuffer>()->GetBackingStore()->Data(); | 
| 707 | 709 | 		int64_t len = args[0].As<v8::ArrayBuffer>()->GetBackingStore()->ByteLength(); | 
| 708 | 710 | 		assert(len >= 0 && len < INT32_MAX); | 
| 709 |  | -		fwrite(data, 1, len, ks->fpw); | 
|  | 711 | +		if (len > 0) fwrite(data, 1, len, ks->fpw); | 
| 710 | 712 | 		args.GetReturnValue().Set((int32_t)len); | 
| 711 | 713 | 	} else if (args[0]->IsString()) { | 
| 712 | 714 | 		int32_t len = args[0].As<v8::String>()->Length(); | 
| 713 | 715 | 		uint8_t *buf; | 
| 714 | 716 | 		buf = K8_MALLOC(uint8_t, len); | 
| 715 | 717 | 		args[0].As<v8::String>()->WriteOneByte(args.GetIsolate(), buf); | 
| 716 |  | -		fwrite(buf, 1, len, ks->fpw); | 
|  | 718 | +		if (len > 0) fwrite(buf, 1, len, ks->fpw); | 
| 717 | 719 | 		free(buf); | 
| 718 | 720 | 		args.GetReturnValue().Set(len); | 
| 719 | 721 | 	} | 
|  | 
0 commit comments