Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

s390/ctcm: Convert sprintf/snprintf to scnprintf

This LWN article explains the why scnprintf is preferred over snprintf
in general
https://lwn.net/Articles/69419/
Ie. snprintf() returns what *would* be the resulting length, while
scnprintf() returns the actual length.

Note that ctcm_print_statistics() writes the data into the kernel log
and is therefore not suitable for sysfs_emit(). Observable behavior is
not changed, as there may be dependencies.

Reviewed-by: Alexandra Winter <wintera@linux.ibm.com>
Signed-off-by: Thorsten Winkler <twinkler@linux.ibm.com>
Signed-off-by: Alexandra Winter <wintera@linux.ibm.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Thorsten Winkler and committed by
Paolo Abeni
1471d85f d585e4b7

+33 -30
+1 -1
drivers/s390/net/ctcm_dbug.c
··· 70 70 if (!debug_level_enabled(ctcm_dbf[dbf_nix].id, level)) 71 71 return; 72 72 va_start(args, fmt); 73 - vsnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args); 73 + vscnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args); 74 74 va_end(args); 75 75 76 76 debug_text_event(ctcm_dbf[dbf_nix].id, level, dbf_txt_buf);
+3 -3
drivers/s390/net/ctcm_main.c
··· 1333 1333 goto nomem_return; 1334 1334 1335 1335 ch->cdev = cdev; 1336 - snprintf(ch->id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev->dev)); 1336 + scnprintf(ch->id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev->dev)); 1337 1337 ch->type = type; 1338 1338 1339 1339 /* ··· 1498 1498 1499 1499 type = get_channel_type(&cdev0->id); 1500 1500 1501 - snprintf(read_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev0->dev)); 1502 - snprintf(write_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev1->dev)); 1501 + scnprintf(read_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev0->dev)); 1502 + scnprintf(write_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev1->dev)); 1503 1503 1504 1504 ret = add_channel(cdev0, type, priv); 1505 1505 if (ret) {
+1
drivers/s390/net/ctcm_main.h
··· 100 100 #define CTCM_PROTO_MPC 4 101 101 #define CTCM_PROTO_MAX 4 102 102 103 + #define CTCM_STATSIZE_LIMIT 64 103 104 #define CTCM_BUFSIZE_LIMIT 65535 104 105 #define CTCM_BUFSIZE_DEFAULT 32768 105 106 #define MPC_BUFSIZE_DEFAULT CTCM_BUFSIZE_LIMIT
+10 -8
drivers/s390/net/ctcm_mpc.c
··· 144 144 145 145 for (ct = 0; ct < len; ct++, ptr++, rptr++) { 146 146 if (sw == 0) { 147 - sprintf(addr, "%16.16llx", (__u64)rptr); 147 + scnprintf(addr, sizeof(addr), "%16.16llx", (__u64)rptr); 148 148 149 - sprintf(boff, "%4.4X", (__u32)ct); 149 + scnprintf(boff, sizeof(boff), "%4.4X", (__u32)ct); 150 150 bhex[0] = '\0'; 151 151 basc[0] = '\0'; 152 152 } ··· 155 155 if (sw == 8) 156 156 strcat(bhex, " "); 157 157 158 - sprintf(tbuf, "%2.2llX", (__u64)*ptr); 158 + scnprintf(tbuf, sizeof(tbuf), "%2.2llX", (__u64)*ptr); 159 159 160 160 tbuf[2] = '\0'; 161 161 strcat(bhex, tbuf); ··· 171 171 continue; 172 172 if ((strcmp(duphex, bhex)) != 0) { 173 173 if (dup != 0) { 174 - sprintf(tdup, 175 - "Duplicate as above to %s", addr); 174 + scnprintf(tdup, sizeof(tdup), 175 + "Duplicate as above to %s", addr); 176 176 ctcm_pr_debug(" --- %s ---\n", 177 177 tdup); 178 178 } ··· 197 197 strcat(basc, " "); 198 198 } 199 199 if (dup != 0) { 200 - sprintf(tdup, "Duplicate as above to %s", addr); 200 + scnprintf(tdup, sizeof(tdup), 201 + "Duplicate as above to %s", addr); 201 202 ctcm_pr_debug(" --- %s ---\n", tdup); 202 203 } 203 204 ctcm_pr_debug(" %s (+%s) : %s [%s]\n", 204 205 addr, boff, bhex, basc); 205 206 } else { 206 207 if (dup >= 1) { 207 - sprintf(tdup, "Duplicate as above to %s", addr); 208 + scnprintf(tdup, sizeof(tdup), 209 + "Duplicate as above to %s", addr); 208 210 ctcm_pr_debug(" --- %s ---\n", tdup); 209 211 } 210 212 if (dup != 0) { ··· 293 291 struct net_device *dev; 294 292 struct ctcm_priv *priv; 295 293 296 - sprintf(device, "%s%i", MPC_DEVICE_NAME, port_num); 294 + scnprintf(device, sizeof(device), "%s%i", MPC_DEVICE_NAME, port_num); 297 295 298 296 dev = __dev_get_by_name(&init_net, device); 299 297
+18 -18
drivers/s390/net/ctcm_sysfs.c
··· 86 86 return; 87 87 p = sbuf; 88 88 89 - p += sprintf(p, " Device FSM state: %s\n", 90 - fsm_getstate_str(priv->fsm)); 91 - p += sprintf(p, " RX channel FSM state: %s\n", 92 - fsm_getstate_str(priv->channel[CTCM_READ]->fsm)); 93 - p += sprintf(p, " TX channel FSM state: %s\n", 94 - fsm_getstate_str(priv->channel[CTCM_WRITE]->fsm)); 95 - p += sprintf(p, " Max. TX buffer used: %ld\n", 96 - priv->channel[WRITE]->prof.maxmulti); 97 - p += sprintf(p, " Max. chained SKBs: %ld\n", 98 - priv->channel[WRITE]->prof.maxcqueue); 99 - p += sprintf(p, " TX single write ops: %ld\n", 100 - priv->channel[WRITE]->prof.doios_single); 101 - p += sprintf(p, " TX multi write ops: %ld\n", 102 - priv->channel[WRITE]->prof.doios_multi); 103 - p += sprintf(p, " Netto bytes written: %ld\n", 104 - priv->channel[WRITE]->prof.txlen); 105 - p += sprintf(p, " Max. TX IO-time: %u\n", 106 - jiffies_to_usecs(priv->channel[WRITE]->prof.tx_time)); 89 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " Device FSM state: %s\n", 90 + fsm_getstate_str(priv->fsm)); 91 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " RX channel FSM state: %s\n", 92 + fsm_getstate_str(priv->channel[CTCM_READ]->fsm)); 93 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " TX channel FSM state: %s\n", 94 + fsm_getstate_str(priv->channel[CTCM_WRITE]->fsm)); 95 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " Max. TX buffer used: %ld\n", 96 + priv->channel[WRITE]->prof.maxmulti); 97 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " Max. chained SKBs: %ld\n", 98 + priv->channel[WRITE]->prof.maxcqueue); 99 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " TX single write ops: %ld\n", 100 + priv->channel[WRITE]->prof.doios_single); 101 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " TX multi write ops: %ld\n", 102 + priv->channel[WRITE]->prof.doios_multi); 103 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " Netto bytes written: %ld\n", 104 + priv->channel[WRITE]->prof.txlen); 105 + p += scnprintf(p, CTCM_STATSIZE_LIMIT, " Max. TX IO-time: %u\n", 106 + jiffies_to_usecs(priv->channel[WRITE]->prof.tx_time)); 107 107 108 108 printk(KERN_INFO "Statistics for %s:\n%s", 109 109 priv->channel[CTCM_WRITE]->netdev->name, sbuf);