Fossil SCM

Remove all calls to sprintf() from makeheaders and mkversion in order to appease prudish compilers.

drh 2023-10-23 10:44 trunk
Commit 8c7bf45096cfc0f51fee3bbc07fa05ea298a1c20cda753592fa6d44835e471b2
--- tools/makeheaders.c
+++ tools/makeheaders.c
@@ -594,11 +594,12 @@
594594
Decl *pDecl;
595595
596596
pDecl = SafeMalloc( sizeof(Decl) + nName + 1);
597597
memset(pDecl,0,sizeof(Decl));
598598
pDecl->zName = (char*)&pDecl[1];
599
- sprintf(pDecl->zName,"%.*s",nName,zName);
599
+ memcpy(pDecl->zName, zName, nName);
600
+ pDecl->zName[nName] = 0;
600601
pDecl->zFile = zFilename;
601602
pDecl->pInclude = includeList;
602603
pDecl->zIf = GetIfString();
603604
InstallDecl(pDecl);
604605
return pDecl;
@@ -627,11 +628,12 @@
627628
return 0;
628629
}
629630
}
630631
pId = SafeMalloc( sizeof(Ident) + nId + 1 );
631632
pId->zName = (char*)&pId[1];
632
- sprintf(pId->zName,"%.*s",nId,zId);
633
+ memcpy(pId->zName, zId, nId);
634
+ pId->zName[nId] = 0;
633635
pId->pNext = pTable->pList;
634636
pTable->pList = pId;
635637
pId->pCollide = pTable->apTable[h];
636638
pTable->apTable[h] = pId;
637639
/* printf("Add to table: %.*s\n",nId,zId); */
@@ -2076,13 +2078,18 @@
20762078
}
20772079
pIf = SafeMalloc( nByte );
20782080
if( zText ){
20792081
pIf->zCondition = (char*)&pIf[1];
20802082
if( zPrefix ){
2081
- sprintf(pIf->zCondition,"%s(%.*s)",zPrefix,nText,zText);
2083
+ int nPrefix = (int)strlen(zPrefix);
2084
+ memcpy(pIf->zCondition, zPrefix, nPrefix);
2085
+ pIf->zCondition[nPrefix] = '(';
2086
+ memcpy(&pIf->zCondition[nPrefix+1], zText, nText);
2087
+ memcpy(&pIf->zCondition[nPrefix+nText+1], ")", 2);
20822088
}else{
2083
- sprintf(pIf->zCondition,"%.*s",nText,zText);
2089
+ memcpy(pIf->zCondition, zText, nText);
2090
+ pIf->zCondition[nText] = 0;
20842091
}
20852092
}else{
20862093
pIf->zCondition = 0;
20872094
}
20882095
pIf->nLine = nLine;
@@ -2154,11 +2161,12 @@
21542161
if( nArg==0 ){ return 0; }
21552162
pDecl = CreateDecl(zArg,nArg);
21562163
pDecl->pComment = pToken->pComment;
21572164
DeclSetProperty(pDecl,TY_Macro);
21582165
pDecl->zDecl = SafeMalloc( pToken->nText + 2 );
2159
- sprintf(pDecl->zDecl,"%.*s\n",pToken->nText,pToken->zText);
2166
+ memcpy(pDecl->zDecl, pToken->zText, pToken->nText);
2167
+ memcpy(&pDecl->zDecl[pToken->nText], "\n", 2);
21602168
if( flags & PS_Export ){
21612169
DeclSetProperty(pDecl,DP_Export);
21622170
}else if( flags & PS_Local ){
21632171
DeclSetProperty(pDecl,DP_Local);
21642172
}
@@ -2183,18 +2191,22 @@
21832191
zIf = GetIfString();
21842192
if( zIf ){
21852193
pInclude = SafeMalloc( sizeof(Include) + nArg*2 + strlen(zIf) + 10 );
21862194
pInclude->zFile = (char*)&pInclude[1];
21872195
pInclude->zLabel = &pInclude->zFile[nArg+1];
2188
- sprintf(pInclude->zFile,"%.*s",nArg,zArg);
2189
- sprintf(pInclude->zLabel,"%.*s:%s",nArg,zArg,zIf);
2196
+ memcpy(pInclude->zFile, zArg, nArg);
2197
+ pInclude->zFile[nArg] = 0;
2198
+ memcpy(pInclude->zLabel, zArg, nArg);
2199
+ pInclude->zLabel[nArg] = ':';
2200
+ memcpy(&pInclude->zLabel[nArg+1], zIf, strlen(zIf)+1);
21902201
pInclude->zIf = &pInclude->zLabel[nArg+1];
21912202
SafeFree(zIf);
21922203
}else{
21932204
pInclude = SafeMalloc( sizeof(Include) + nArg + 1 );
21942205
pInclude->zFile = (char*)&pInclude[1];
2195
- sprintf(pInclude->zFile,"%.*s",nArg,zArg);
2206
+ memcpy(pInclude->zFile, zArg, nArg);
2207
+ pInclude->zFile[nArg] = 0;
21962208
pInclude->zIf = 0;
21972209
pInclude->zLabel = pInclude->zFile;
21982210
}
21992211
pInclude->pNext = includeList;
22002212
includeList = pInclude;
22012213
--- tools/makeheaders.c
+++ tools/makeheaders.c
@@ -594,11 +594,12 @@
594 Decl *pDecl;
595
596 pDecl = SafeMalloc( sizeof(Decl) + nName + 1);
597 memset(pDecl,0,sizeof(Decl));
598 pDecl->zName = (char*)&pDecl[1];
599 sprintf(pDecl->zName,"%.*s",nName,zName);
 
600 pDecl->zFile = zFilename;
601 pDecl->pInclude = includeList;
602 pDecl->zIf = GetIfString();
603 InstallDecl(pDecl);
604 return pDecl;
@@ -627,11 +628,12 @@
627 return 0;
628 }
629 }
630 pId = SafeMalloc( sizeof(Ident) + nId + 1 );
631 pId->zName = (char*)&pId[1];
632 sprintf(pId->zName,"%.*s",nId,zId);
 
633 pId->pNext = pTable->pList;
634 pTable->pList = pId;
635 pId->pCollide = pTable->apTable[h];
636 pTable->apTable[h] = pId;
637 /* printf("Add to table: %.*s\n",nId,zId); */
@@ -2076,13 +2078,18 @@
2076 }
2077 pIf = SafeMalloc( nByte );
2078 if( zText ){
2079 pIf->zCondition = (char*)&pIf[1];
2080 if( zPrefix ){
2081 sprintf(pIf->zCondition,"%s(%.*s)",zPrefix,nText,zText);
 
 
 
 
2082 }else{
2083 sprintf(pIf->zCondition,"%.*s",nText,zText);
 
2084 }
2085 }else{
2086 pIf->zCondition = 0;
2087 }
2088 pIf->nLine = nLine;
@@ -2154,11 +2161,12 @@
2154 if( nArg==0 ){ return 0; }
2155 pDecl = CreateDecl(zArg,nArg);
2156 pDecl->pComment = pToken->pComment;
2157 DeclSetProperty(pDecl,TY_Macro);
2158 pDecl->zDecl = SafeMalloc( pToken->nText + 2 );
2159 sprintf(pDecl->zDecl,"%.*s\n",pToken->nText,pToken->zText);
 
2160 if( flags & PS_Export ){
2161 DeclSetProperty(pDecl,DP_Export);
2162 }else if( flags & PS_Local ){
2163 DeclSetProperty(pDecl,DP_Local);
2164 }
@@ -2183,18 +2191,22 @@
2183 zIf = GetIfString();
2184 if( zIf ){
2185 pInclude = SafeMalloc( sizeof(Include) + nArg*2 + strlen(zIf) + 10 );
2186 pInclude->zFile = (char*)&pInclude[1];
2187 pInclude->zLabel = &pInclude->zFile[nArg+1];
2188 sprintf(pInclude->zFile,"%.*s",nArg,zArg);
2189 sprintf(pInclude->zLabel,"%.*s:%s",nArg,zArg,zIf);
 
 
 
2190 pInclude->zIf = &pInclude->zLabel[nArg+1];
2191 SafeFree(zIf);
2192 }else{
2193 pInclude = SafeMalloc( sizeof(Include) + nArg + 1 );
2194 pInclude->zFile = (char*)&pInclude[1];
2195 sprintf(pInclude->zFile,"%.*s",nArg,zArg);
 
2196 pInclude->zIf = 0;
2197 pInclude->zLabel = pInclude->zFile;
2198 }
2199 pInclude->pNext = includeList;
2200 includeList = pInclude;
2201
--- tools/makeheaders.c
+++ tools/makeheaders.c
@@ -594,11 +594,12 @@
594 Decl *pDecl;
595
596 pDecl = SafeMalloc( sizeof(Decl) + nName + 1);
597 memset(pDecl,0,sizeof(Decl));
598 pDecl->zName = (char*)&pDecl[1];
599 memcpy(pDecl->zName, zName, nName);
600 pDecl->zName[nName] = 0;
601 pDecl->zFile = zFilename;
602 pDecl->pInclude = includeList;
603 pDecl->zIf = GetIfString();
604 InstallDecl(pDecl);
605 return pDecl;
@@ -627,11 +628,12 @@
628 return 0;
629 }
630 }
631 pId = SafeMalloc( sizeof(Ident) + nId + 1 );
632 pId->zName = (char*)&pId[1];
633 memcpy(pId->zName, zId, nId);
634 pId->zName[nId] = 0;
635 pId->pNext = pTable->pList;
636 pTable->pList = pId;
637 pId->pCollide = pTable->apTable[h];
638 pTable->apTable[h] = pId;
639 /* printf("Add to table: %.*s\n",nId,zId); */
@@ -2076,13 +2078,18 @@
2078 }
2079 pIf = SafeMalloc( nByte );
2080 if( zText ){
2081 pIf->zCondition = (char*)&pIf[1];
2082 if( zPrefix ){
2083 int nPrefix = (int)strlen(zPrefix);
2084 memcpy(pIf->zCondition, zPrefix, nPrefix);
2085 pIf->zCondition[nPrefix] = '(';
2086 memcpy(&pIf->zCondition[nPrefix+1], zText, nText);
2087 memcpy(&pIf->zCondition[nPrefix+nText+1], ")", 2);
2088 }else{
2089 memcpy(pIf->zCondition, zText, nText);
2090 pIf->zCondition[nText] = 0;
2091 }
2092 }else{
2093 pIf->zCondition = 0;
2094 }
2095 pIf->nLine = nLine;
@@ -2154,11 +2161,12 @@
2161 if( nArg==0 ){ return 0; }
2162 pDecl = CreateDecl(zArg,nArg);
2163 pDecl->pComment = pToken->pComment;
2164 DeclSetProperty(pDecl,TY_Macro);
2165 pDecl->zDecl = SafeMalloc( pToken->nText + 2 );
2166 memcpy(pDecl->zDecl, pToken->zText, pToken->nText);
2167 memcpy(&pDecl->zDecl[pToken->nText], "\n", 2);
2168 if( flags & PS_Export ){
2169 DeclSetProperty(pDecl,DP_Export);
2170 }else if( flags & PS_Local ){
2171 DeclSetProperty(pDecl,DP_Local);
2172 }
@@ -2183,18 +2191,22 @@
2191 zIf = GetIfString();
2192 if( zIf ){
2193 pInclude = SafeMalloc( sizeof(Include) + nArg*2 + strlen(zIf) + 10 );
2194 pInclude->zFile = (char*)&pInclude[1];
2195 pInclude->zLabel = &pInclude->zFile[nArg+1];
2196 memcpy(pInclude->zFile, zArg, nArg);
2197 pInclude->zFile[nArg] = 0;
2198 memcpy(pInclude->zLabel, zArg, nArg);
2199 pInclude->zLabel[nArg] = ':';
2200 memcpy(&pInclude->zLabel[nArg+1], zIf, strlen(zIf)+1);
2201 pInclude->zIf = &pInclude->zLabel[nArg+1];
2202 SafeFree(zIf);
2203 }else{
2204 pInclude = SafeMalloc( sizeof(Include) + nArg + 1 );
2205 pInclude->zFile = (char*)&pInclude[1];
2206 memcpy(pInclude->zFile, zArg, nArg);
2207 pInclude->zFile[nArg] = 0;
2208 pInclude->zIf = 0;
2209 pInclude->zLabel = pInclude->zFile;
2210 }
2211 pInclude->pNext = includeList;
2212 includeList = pInclude;
2213
--- tools/mkversion.c
+++ tools/mkversion.c
@@ -113,17 +113,18 @@
113113
printf("#define MANIFEST_VERSION \"[%10.10s]\"\n",b);
114114
n = strlen(b);
115115
if( n + 50 < sizeof(b) ){
116116
#ifdef FOSSIL_BUILD_EPOCH
117117
#define str(s) #s
118
- sprintf(b+n, "%d", (int)strtoll(str(FOSSIL_BUILD_EPOCH), 0, 10));
118
+ snprintf(b+n, sizeof(b)-n,
119
+ "%d", (int)strtoll(str(FOSSIL_BUILD_EPOCH), 0, 10));
119120
#else
120121
const char *zEpoch = getenv("SOURCE_DATE_EPOCH");
121122
if( zEpoch && isdigit(zEpoch[0]) ){
122
- sprintf(b+n, "%d", (int)strtoll(zEpoch, 0, 10));
123
+ snprintf(b+n, sizeof(b)-n, "%d", (int)strtoll(zEpoch, 0, 10));
123124
}else{
124
- sprintf(b+n, "%d", (int)time(0));
125
+ snprintf(b+n, sizeof(b)-n, "%d", (int)time(0));
125126
}
126127
#endif
127128
hash(b,33,vx);
128129
printf("#define FOSSIL_BUILD_HASH \"%s\"\n", vx);
129130
}
130131
--- tools/mkversion.c
+++ tools/mkversion.c
@@ -113,17 +113,18 @@
113 printf("#define MANIFEST_VERSION \"[%10.10s]\"\n",b);
114 n = strlen(b);
115 if( n + 50 < sizeof(b) ){
116 #ifdef FOSSIL_BUILD_EPOCH
117 #define str(s) #s
118 sprintf(b+n, "%d", (int)strtoll(str(FOSSIL_BUILD_EPOCH), 0, 10));
 
119 #else
120 const char *zEpoch = getenv("SOURCE_DATE_EPOCH");
121 if( zEpoch && isdigit(zEpoch[0]) ){
122 sprintf(b+n, "%d", (int)strtoll(zEpoch, 0, 10));
123 }else{
124 sprintf(b+n, "%d", (int)time(0));
125 }
126 #endif
127 hash(b,33,vx);
128 printf("#define FOSSIL_BUILD_HASH \"%s\"\n", vx);
129 }
130
--- tools/mkversion.c
+++ tools/mkversion.c
@@ -113,17 +113,18 @@
113 printf("#define MANIFEST_VERSION \"[%10.10s]\"\n",b);
114 n = strlen(b);
115 if( n + 50 < sizeof(b) ){
116 #ifdef FOSSIL_BUILD_EPOCH
117 #define str(s) #s
118 snprintf(b+n, sizeof(b)-n,
119 "%d", (int)strtoll(str(FOSSIL_BUILD_EPOCH), 0, 10));
120 #else
121 const char *zEpoch = getenv("SOURCE_DATE_EPOCH");
122 if( zEpoch && isdigit(zEpoch[0]) ){
123 snprintf(b+n, sizeof(b)-n, "%d", (int)strtoll(zEpoch, 0, 10));
124 }else{
125 snprintf(b+n, sizeof(b)-n, "%d", (int)time(0));
126 }
127 #endif
128 hash(b,33,vx);
129 printf("#define FOSSIL_BUILD_HASH \"%s\"\n", vx);
130 }
131

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button