aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Guryanov <caiiiycuk@gmail.com>2012-05-29 20:07:28 +0700
committerAleksander Guryanov <caiiiycuk@gmail.com>2012-05-29 20:07:28 +0700
commitedb0920e82300069f8b0ccdc405463f2e44079d8 (patch)
treee89f44bb4e0b9f1f09772304750866bb6e8ba269
parentb4bfabda973af6a14ebee6de1191a476feeed125 (diff)
Fix behavior strndup whern size <= 0, in this case strndup returns empty string
-rw-r--r--src/library.js8
-rwxr-xr-xtests/runner.py20
2 files changed, 20 insertions, 8 deletions
diff --git a/src/library.js b/src/library.js
index c353380c..8d68251f 100644
--- a/src/library.js
+++ b/src/library.js
@@ -4203,8 +4203,12 @@ LibraryManager.library = {
strndup: function(ptr, size) {
var len = String_len(ptr);
- if (size <= 0 || size >= len) {
- return _strdup(ptr);
+ if (size >= len) {
+ return _strdup(ptr);
+ }
+
+ if (size < 0) {
+ size = 0;
}
var newStr = _malloc(size + 1);
diff --git a/tests/runner.py b/tests/runner.py
index 4064af15..cc3811a2 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -1406,26 +1406,34 @@ m_divisor is 1091269979
int main(int argc, char **argv) {
const char* source = "strndup - duplicate a specific number of bytes from a string";
- char* strdup_val = strndup(source, 7);
- printf("%s\\n", strdup_val);
+ char* strdup_val = strndup(source, 0);
+ printf("1:%s\\n", strdup_val);
+ free(strdup_val);
+
+ strdup_val = strndup(source, 7);
+ printf("2:%s\\n", strdup_val);
free(strdup_val);
strdup_val = strndup(source, 1000);
- printf("%s\\n", strdup_val);
+ printf("3:%s\\n", strdup_val);
free(strdup_val);
strdup_val = strndup(source, 60);
- printf("%s\\n", strdup_val);
+ printf("4:%s\\n", strdup_val);
free(strdup_val);
strdup_val = strndup(source, 19);
- printf("%s\\n", strdup_val);
+ printf("5:%s\\n", strdup_val);
+ free(strdup_val);
+
+ strdup_val = strndup(source, -1);
+ printf("6:%s\\n", strdup_val);
free(strdup_val);
return 0;
}
'''
- self.do_run(src, 'strndup\nstrndup - duplicate a specific number of bytes from a string\nstrndup - duplicate a specific number of bytes from a string\nstrndup - duplicate\n')
+ self.do_run(src, '1:\n2:strndup\n3:strndup - duplicate a specific number of bytes from a string\n4:strndup - duplicate a specific number of bytes from a string\n5:strndup - duplicate\n6:\n')
def test_errar(self):
src = r'''