diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-20 14:36:32 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-20 14:36:32 -0200 |
| commit | 8b88ab07f7cfc216407a88d75ad8f0546224c8d7 (patch) | |
| tree | 71acdcf4416d114087419863f45186270d5c8d15 /lobject.c | |
| parent | 2779ceeb125e603ea667171d9362e0b766b7abae (diff) | |
| download | lua-8b88ab07f7cfc216407a88d75ad8f0546224c8d7.tar.gz lua-8b88ab07f7cfc216407a88d75ad8f0546224c8d7.tar.bz2 lua-8b88ab07f7cfc216407a88d75ad8f0546224c8d7.zip | |
more controled use of `sprintf'
Diffstat (limited to 'lobject.c')
| -rw-r--r-- | lobject.c | 27 |
1 files changed, 18 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 1.53 2000/10/09 13:47:32 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 1.54 2000/10/10 19:53:20 roberto Exp roberto $ |
| 3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -76,10 +76,13 @@ int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */ | |||
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | 78 | ||
| 79 | /* maximum length of a string format for `luaO_verror' */ | ||
| 80 | #define MAX_VERROR 280 | ||
| 81 | |||
| 79 | /* this function needs to handle only '%d' and '%.XXs' formats */ | 82 | /* this function needs to handle only '%d' and '%.XXs' formats */ |
| 80 | void luaO_verror (lua_State *L, const char *fmt, ...) { | 83 | void luaO_verror (lua_State *L, const char *fmt, ...) { |
| 81 | va_list argp; | 84 | va_list argp; |
| 82 | char buff[600]; /* to hold formatted message */ | 85 | char buff[MAX_VERROR]; /* to hold formatted message */ |
| 83 | va_start(argp, fmt); | 86 | va_start(argp, fmt); |
| 84 | vsprintf(buff, fmt, argp); | 87 | vsprintf(buff, fmt, argp); |
| 85 | va_end(argp); | 88 | va_end(argp); |
| @@ -88,8 +91,10 @@ void luaO_verror (lua_State *L, const char *fmt, ...) { | |||
| 88 | 91 | ||
| 89 | 92 | ||
| 90 | void luaO_chunkid (char *out, const char *source, int bufflen) { | 93 | void luaO_chunkid (char *out, const char *source, int bufflen) { |
| 91 | if (*source == '=') | 94 | if (*source == '=') { |
| 92 | sprintf(out, "%.*s", bufflen-1, source+1); /* remove first char */ | 95 | strncpy(out, source+1, bufflen); /* remove first char */ |
| 96 | out[bufflen-1] = '\0'; /* ensures null termination */ | ||
| 97 | } | ||
| 93 | else { | 98 | else { |
| 94 | if (*source == '@') { | 99 | if (*source == '@') { |
| 95 | int l; | 100 | int l; |
| @@ -98,19 +103,23 @@ void luaO_chunkid (char *out, const char *source, int bufflen) { | |||
| 98 | l = strlen(source); | 103 | l = strlen(source); |
| 99 | if (l>bufflen) { | 104 | if (l>bufflen) { |
| 100 | source += (l-bufflen); /* get last part of file name */ | 105 | source += (l-bufflen); /* get last part of file name */ |
| 101 | sprintf(out, "file `...%s'", source); | 106 | sprintf(out, "file `...%.99s'", source); |
| 102 | } | 107 | } |
| 103 | else | 108 | else |
| 104 | sprintf(out, "file `%s'", source); | 109 | sprintf(out, "file `%.99s'", source); |
| 105 | } | 110 | } |
| 106 | else { | 111 | else { |
| 107 | int len = strcspn(source, "\n"); /* stop at first newline */ | 112 | int len = strcspn(source, "\n"); /* stop at first newline */ |
| 108 | bufflen -= sizeof("string \"%.*s...\""); | 113 | bufflen -= sizeof("string \"%.*s...\""); |
| 109 | if (len > bufflen) len = bufflen; | 114 | if (len > bufflen) len = bufflen; |
| 110 | if (source[len] != '\0') /* must truncate? */ | 115 | if (source[len] != '\0') { /* must truncate? */ |
| 111 | sprintf(out, "string \"%.*s...\"", len, source); | 116 | strcpy(out, "string \""); |
| 117 | out += strlen(out); | ||
| 118 | strncpy(out, source, len); | ||
| 119 | strcpy(out+len, "...\""); | ||
| 120 | } | ||
| 112 | else | 121 | else |
| 113 | sprintf(out, "string \"%s\"", source); | 122 | sprintf(out, "string \"%.99s\"", source); |
| 114 | } | 123 | } |
| 115 | } | 124 | } |
| 116 | } | 125 | } |
