Is there a stricter strtoull() in any ubiquitous C library?

I want a function that will interpret a string as a strictly unsigned integer, failing when the string overflows, represents a negative number, or does not represent a number.

strtoull() does set errno to ERANGE on overflow or EINVAL for a string that does not begin with a number, but accepts negative numbers as valid.

strtoumax() is the same as strtoull()

atoll() and strtonum() are ruled out because they are strictly for signed integers.

sscanf() sets errno to ERANGE on overflow and stops processing the string if it does not begin with a number (reflected in sscanf’s return value), but also accepts negative numbers as valid.

Is there another option that I’m missing?

2

There are no out-of-the-box solutions that fit your requirements, but it is easy enough to write such a function yourself, using strtoull or strtoll as basis.

For negative numbers, either you can check for the minus sign beforehand, or you can convert to a signed number first and report an error for negative values.
For numbers followed by other characters, you can check that the endp pointer that you pass to strto(u)ll points to the end of the string.

4

This should work, unless I’m missing something. Define yourself these thin wrappers around standard C functions:

inline uintmax_t
strtoumax_noneg(const char *numstr, char **restrict endptr, int base)
{
    if (strtol(numstr, endptr, base) < 0) {
        errno = ERANGE;
        return 0;
    }
    return strtoumax(numstr, endptr, base);
}


inline unsigned long long
strtoull_noneg(const char *numstr, char **restrict endptr, int base)
{
    if (strtol(numstr, endptr, base) < 0) {
        errno = ERANGE;
        return 0;
    }
    return strtoull(numstr, endptr, base);
}


inline unsigned long
strtoul_noneg(const char *numstr, char **restrict endptr, int base)
{
    if (strtol(numstr, endptr, base) < 0) {
        errno = ERANGE;
        return 0;
    }
    return strtoul(numstr, endptr, base);
}

If strtol(3) underflows, it returns LONG_MIN, which is < 0, so this rejects all negative integers.

(Edited to add strtoull_noneg() after @ShadSterling ‘s comment.)

5

You are describing two separate problems. One is that the string should be in a specific form, namely all digits (but you didn’t specify whether to permit leading and trailing spaces). The second is that the range should not overflow. There is no readily available function that provides exactly this combination.

The first requirement is best satisfied by testing (or parsing) the string before decoding it. In most languages I would use a regular expression. In C++ you can use library functions. In C you write loops and test characters.

The second requirement is actually quite tricky. You can include overflow testing in your decode function but testing against MAXUINT or something similar needs some careful coding. A simple solution is to decode as a double and then test the range before casting to uint.

In summary, when you have requirements as specific as this you normally finish up writing your own functions, perhaps basing it on available “free” source code. Over the 30 years I’ve been writing C code I’ve probably written this function or similar about 5 or 6 times. It gets easier with practice!

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật